OSDN Git Service

PR debug/25023
[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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-codes.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "except.h"
39 #include "function.h"
40 #include "recog.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "langhooks.h"
49 #include "cgraph.h"
50 #include "tree-gimple.h"
51 #include "dwarf2.h"
52
53 #ifndef CHECK_STACK_LIMIT
54 #define CHECK_STACK_LIMIT (-1)
55 #endif
56
57 /* Return index of given mode in mult and division cost tables.  */
58 #define MODE_INDEX(mode)                                        \
59   ((mode) == QImode ? 0                                         \
60    : (mode) == HImode ? 1                                       \
61    : (mode) == SImode ? 2                                       \
62    : (mode) == DImode ? 3                                       \
63    : 4)
64
65 /* Processor costs (relative to an add) */
66 static const
67 struct processor_costs size_cost = {    /* costs for tunning for size */
68   2,                                    /* cost of an add instruction */
69   3,                                    /* cost of a lea instruction */
70   2,                                    /* variable shift costs */
71   3,                                    /* constant shift costs */
72   {3, 3, 3, 3, 5},                      /* cost of starting a multiply */
73   0,                                    /* cost of multiply per each bit set */
74   {3, 3, 3, 3, 5},                      /* cost of a divide/mod */
75   3,                                    /* cost of movsx */
76   3,                                    /* cost of movzx */
77   0,                                    /* "large" insn */
78   2,                                    /* MOVE_RATIO */
79   2,                                    /* cost for loading QImode using movzbl */
80   {2, 2, 2},                            /* cost of loading integer registers
81                                            in QImode, HImode and SImode.
82                                            Relative to reg-reg move (2).  */
83   {2, 2, 2},                            /* cost of storing integer registers */
84   2,                                    /* cost of reg,reg fld/fst */
85   {2, 2, 2},                            /* cost of loading fp registers
86                                            in SFmode, DFmode and XFmode */
87   {2, 2, 2},                            /* cost of loading integer registers */
88   3,                                    /* cost of moving MMX register */
89   {3, 3},                               /* cost of loading MMX registers
90                                            in SImode and DImode */
91   {3, 3},                               /* cost of storing MMX registers
92                                            in SImode and DImode */
93   3,                                    /* cost of moving SSE register */
94   {3, 3, 3},                            /* cost of loading SSE registers
95                                            in SImode, DImode and TImode */
96   {3, 3, 3},                            /* cost of storing SSE registers
97                                            in SImode, DImode and TImode */
98   3,                                    /* MMX or SSE register to integer */
99   0,                                    /* size of prefetch block */
100   0,                                    /* number of parallel prefetches */
101   1,                                    /* Branch cost */
102   2,                                    /* cost of FADD and FSUB insns.  */
103   2,                                    /* cost of FMUL instruction.  */
104   2,                                    /* cost of FDIV instruction.  */
105   2,                                    /* cost of FABS instruction.  */
106   2,                                    /* cost of FCHS instruction.  */
107   2,                                    /* cost of FSQRT instruction.  */
108 };
109
110 /* Processor costs (relative to an add) */
111 static const
112 struct processor_costs i386_cost = {    /* 386 specific costs */
113   1,                                    /* cost of an add instruction */
114   1,                                    /* cost of a lea instruction */
115   3,                                    /* variable shift costs */
116   2,                                    /* constant shift costs */
117   {6, 6, 6, 6, 6},                      /* cost of starting a multiply */
118   1,                                    /* cost of multiply per each bit set */
119   {23, 23, 23, 23, 23},                 /* cost of a divide/mod */
120   3,                                    /* cost of movsx */
121   2,                                    /* cost of movzx */
122   15,                                   /* "large" insn */
123   3,                                    /* MOVE_RATIO */
124   4,                                    /* cost for loading QImode using movzbl */
125   {2, 4, 2},                            /* cost of loading integer registers
126                                            in QImode, HImode and SImode.
127                                            Relative to reg-reg move (2).  */
128   {2, 4, 2},                            /* cost of storing integer registers */
129   2,                                    /* cost of reg,reg fld/fst */
130   {8, 8, 8},                            /* cost of loading fp registers
131                                            in SFmode, DFmode and XFmode */
132   {8, 8, 8},                            /* cost of loading integer registers */
133   2,                                    /* cost of moving MMX register */
134   {4, 8},                               /* cost of loading MMX registers
135                                            in SImode and DImode */
136   {4, 8},                               /* cost of storing MMX registers
137                                            in SImode and DImode */
138   2,                                    /* cost of moving SSE register */
139   {4, 8, 16},                           /* cost of loading SSE registers
140                                            in SImode, DImode and TImode */
141   {4, 8, 16},                           /* cost of storing SSE registers
142                                            in SImode, DImode and TImode */
143   3,                                    /* MMX or SSE register to integer */
144   0,                                    /* size of prefetch block */
145   0,                                    /* number of parallel prefetches */
146   1,                                    /* Branch cost */
147   23,                                   /* cost of FADD and FSUB insns.  */
148   27,                                   /* cost of FMUL instruction.  */
149   88,                                   /* cost of FDIV instruction.  */
150   22,                                   /* cost of FABS instruction.  */
151   24,                                   /* cost of FCHS instruction.  */
152   122,                                  /* cost of FSQRT instruction.  */
153 };
154
155 static const
156 struct processor_costs i486_cost = {    /* 486 specific costs */
157   1,                                    /* cost of an add instruction */
158   1,                                    /* cost of a lea instruction */
159   3,                                    /* variable shift costs */
160   2,                                    /* constant shift costs */
161   {12, 12, 12, 12, 12},                 /* cost of starting a multiply */
162   1,                                    /* cost of multiply per each bit set */
163   {40, 40, 40, 40, 40},                 /* cost of a divide/mod */
164   3,                                    /* cost of movsx */
165   2,                                    /* cost of movzx */
166   15,                                   /* "large" insn */
167   3,                                    /* MOVE_RATIO */
168   4,                                    /* cost for loading QImode using movzbl */
169   {2, 4, 2},                            /* cost of loading integer registers
170                                            in QImode, HImode and SImode.
171                                            Relative to reg-reg move (2).  */
172   {2, 4, 2},                            /* cost of storing integer registers */
173   2,                                    /* cost of reg,reg fld/fst */
174   {8, 8, 8},                            /* cost of loading fp registers
175                                            in SFmode, DFmode and XFmode */
176   {8, 8, 8},                            /* cost of loading integer registers */
177   2,                                    /* cost of moving MMX register */
178   {4, 8},                               /* cost of loading MMX registers
179                                            in SImode and DImode */
180   {4, 8},                               /* cost of storing MMX registers
181                                            in SImode and DImode */
182   2,                                    /* cost of moving SSE register */
183   {4, 8, 16},                           /* cost of loading SSE registers
184                                            in SImode, DImode and TImode */
185   {4, 8, 16},                           /* cost of storing SSE registers
186                                            in SImode, DImode and TImode */
187   3,                                    /* MMX or SSE register to integer */
188   0,                                    /* size of prefetch block */
189   0,                                    /* number of parallel prefetches */
190   1,                                    /* Branch cost */
191   8,                                    /* cost of FADD and FSUB insns.  */
192   16,                                   /* cost of FMUL instruction.  */
193   73,                                   /* cost of FDIV instruction.  */
194   3,                                    /* cost of FABS instruction.  */
195   3,                                    /* cost of FCHS instruction.  */
196   83,                                   /* cost of FSQRT instruction.  */
197 };
198
199 static const
200 struct processor_costs pentium_cost = {
201   1,                                    /* cost of an add instruction */
202   1,                                    /* cost of a lea instruction */
203   4,                                    /* variable shift costs */
204   1,                                    /* constant shift costs */
205   {11, 11, 11, 11, 11},                 /* cost of starting a multiply */
206   0,                                    /* cost of multiply per each bit set */
207   {25, 25, 25, 25, 25},                 /* cost of a divide/mod */
208   3,                                    /* cost of movsx */
209   2,                                    /* cost of movzx */
210   8,                                    /* "large" insn */
211   6,                                    /* MOVE_RATIO */
212   6,                                    /* cost for loading QImode using movzbl */
213   {2, 4, 2},                            /* cost of loading integer registers
214                                            in QImode, HImode and SImode.
215                                            Relative to reg-reg move (2).  */
216   {2, 4, 2},                            /* cost of storing integer registers */
217   2,                                    /* cost of reg,reg fld/fst */
218   {2, 2, 6},                            /* cost of loading fp registers
219                                            in SFmode, DFmode and XFmode */
220   {4, 4, 6},                            /* cost of loading integer registers */
221   8,                                    /* cost of moving MMX register */
222   {8, 8},                               /* cost of loading MMX registers
223                                            in SImode and DImode */
224   {8, 8},                               /* cost of storing MMX registers
225                                            in SImode and DImode */
226   2,                                    /* cost of moving SSE register */
227   {4, 8, 16},                           /* cost of loading SSE registers
228                                            in SImode, DImode and TImode */
229   {4, 8, 16},                           /* cost of storing SSE registers
230                                            in SImode, DImode and TImode */
231   3,                                    /* MMX or SSE register to integer */
232   0,                                    /* size of prefetch block */
233   0,                                    /* number of parallel prefetches */
234   2,                                    /* Branch cost */
235   3,                                    /* cost of FADD and FSUB insns.  */
236   3,                                    /* cost of FMUL instruction.  */
237   39,                                   /* cost of FDIV instruction.  */
238   1,                                    /* cost of FABS instruction.  */
239   1,                                    /* cost of FCHS instruction.  */
240   70,                                   /* cost of FSQRT instruction.  */
241 };
242
243 static const
244 struct processor_costs pentiumpro_cost = {
245   1,                                    /* cost of an add instruction */
246   1,                                    /* cost of a lea instruction */
247   1,                                    /* variable shift costs */
248   1,                                    /* constant shift costs */
249   {4, 4, 4, 4, 4},                      /* cost of starting a multiply */
250   0,                                    /* cost of multiply per each bit set */
251   {17, 17, 17, 17, 17},                 /* cost of a divide/mod */
252   1,                                    /* cost of movsx */
253   1,                                    /* cost of movzx */
254   8,                                    /* "large" insn */
255   6,                                    /* MOVE_RATIO */
256   2,                                    /* cost for loading QImode using movzbl */
257   {4, 4, 4},                            /* cost of loading integer registers
258                                            in QImode, HImode and SImode.
259                                            Relative to reg-reg move (2).  */
260   {2, 2, 2},                            /* cost of storing integer registers */
261   2,                                    /* cost of reg,reg fld/fst */
262   {2, 2, 6},                            /* cost of loading fp registers
263                                            in SFmode, DFmode and XFmode */
264   {4, 4, 6},                            /* cost of loading integer registers */
265   2,                                    /* cost of moving MMX register */
266   {2, 2},                               /* cost of loading MMX registers
267                                            in SImode and DImode */
268   {2, 2},                               /* cost of storing MMX registers
269                                            in SImode and DImode */
270   2,                                    /* cost of moving SSE register */
271   {2, 2, 8},                            /* cost of loading SSE registers
272                                            in SImode, DImode and TImode */
273   {2, 2, 8},                            /* cost of storing SSE registers
274                                            in SImode, DImode and TImode */
275   3,                                    /* MMX or SSE register to integer */
276   32,                                   /* size of prefetch block */
277   6,                                    /* number of parallel prefetches */
278   2,                                    /* Branch cost */
279   3,                                    /* cost of FADD and FSUB insns.  */
280   5,                                    /* cost of FMUL instruction.  */
281   56,                                   /* cost of FDIV instruction.  */
282   2,                                    /* cost of FABS instruction.  */
283   2,                                    /* cost of FCHS instruction.  */
284   56,                                   /* cost of FSQRT instruction.  */
285 };
286
287 static const
288 struct processor_costs k6_cost = {
289   1,                                    /* cost of an add instruction */
290   2,                                    /* cost of a lea instruction */
291   1,                                    /* variable shift costs */
292   1,                                    /* constant shift costs */
293   {3, 3, 3, 3, 3},                      /* cost of starting a multiply */
294   0,                                    /* cost of multiply per each bit set */
295   {18, 18, 18, 18, 18},                 /* cost of a divide/mod */
296   2,                                    /* cost of movsx */
297   2,                                    /* cost of movzx */
298   8,                                    /* "large" insn */
299   4,                                    /* MOVE_RATIO */
300   3,                                    /* cost for loading QImode using movzbl */
301   {4, 5, 4},                            /* cost of loading integer registers
302                                            in QImode, HImode and SImode.
303                                            Relative to reg-reg move (2).  */
304   {2, 3, 2},                            /* cost of storing integer registers */
305   4,                                    /* cost of reg,reg fld/fst */
306   {6, 6, 6},                            /* cost of loading fp registers
307                                            in SFmode, DFmode and XFmode */
308   {4, 4, 4},                            /* cost of loading integer registers */
309   2,                                    /* cost of moving MMX register */
310   {2, 2},                               /* cost of loading MMX registers
311                                            in SImode and DImode */
312   {2, 2},                               /* cost of storing MMX registers
313                                            in SImode and DImode */
314   2,                                    /* cost of moving SSE register */
315   {2, 2, 8},                            /* cost of loading SSE registers
316                                            in SImode, DImode and TImode */
317   {2, 2, 8},                            /* cost of storing SSE registers
318                                            in SImode, DImode and TImode */
319   6,                                    /* MMX or SSE register to integer */
320   32,                                   /* size of prefetch block */
321   1,                                    /* number of parallel prefetches */
322   1,                                    /* Branch cost */
323   2,                                    /* cost of FADD and FSUB insns.  */
324   2,                                    /* cost of FMUL instruction.  */
325   56,                                   /* cost of FDIV instruction.  */
326   2,                                    /* cost of FABS instruction.  */
327   2,                                    /* cost of FCHS instruction.  */
328   56,                                   /* cost of FSQRT instruction.  */
329 };
330
331 static const
332 struct processor_costs athlon_cost = {
333   1,                                    /* cost of an add instruction */
334   2,                                    /* cost of a lea instruction */
335   1,                                    /* variable shift costs */
336   1,                                    /* constant shift costs */
337   {5, 5, 5, 5, 5},                      /* cost of starting a multiply */
338   0,                                    /* cost of multiply per each bit set */
339   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
340   1,                                    /* cost of movsx */
341   1,                                    /* cost of movzx */
342   8,                                    /* "large" insn */
343   9,                                    /* MOVE_RATIO */
344   4,                                    /* cost for loading QImode using movzbl */
345   {3, 4, 3},                            /* cost of loading integer registers
346                                            in QImode, HImode and SImode.
347                                            Relative to reg-reg move (2).  */
348   {3, 4, 3},                            /* cost of storing integer registers */
349   4,                                    /* cost of reg,reg fld/fst */
350   {4, 4, 12},                           /* cost of loading fp registers
351                                            in SFmode, DFmode and XFmode */
352   {6, 6, 8},                            /* cost of loading integer registers */
353   2,                                    /* cost of moving MMX register */
354   {4, 4},                               /* cost of loading MMX registers
355                                            in SImode and DImode */
356   {4, 4},                               /* cost of storing MMX registers
357                                            in SImode and DImode */
358   2,                                    /* cost of moving SSE register */
359   {4, 4, 6},                            /* cost of loading SSE registers
360                                            in SImode, DImode and TImode */
361   {4, 4, 5},                            /* cost of storing SSE registers
362                                            in SImode, DImode and TImode */
363   5,                                    /* MMX or SSE register to integer */
364   64,                                   /* size of prefetch block */
365   6,                                    /* number of parallel prefetches */
366   5,                                    /* Branch cost */
367   4,                                    /* cost of FADD and FSUB insns.  */
368   4,                                    /* cost of FMUL instruction.  */
369   24,                                   /* cost of FDIV instruction.  */
370   2,                                    /* cost of FABS instruction.  */
371   2,                                    /* cost of FCHS instruction.  */
372   35,                                   /* cost of FSQRT instruction.  */
373 };
374
375 static const
376 struct processor_costs k8_cost = {
377   1,                                    /* cost of an add instruction */
378   2,                                    /* cost of a lea instruction */
379   1,                                    /* variable shift costs */
380   1,                                    /* constant shift costs */
381   {3, 4, 3, 4, 5},                      /* cost of starting a multiply */
382   0,                                    /* cost of multiply per each bit set */
383   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
384   1,                                    /* cost of movsx */
385   1,                                    /* cost of movzx */
386   8,                                    /* "large" insn */
387   9,                                    /* MOVE_RATIO */
388   4,                                    /* cost for loading QImode using movzbl */
389   {3, 4, 3},                            /* cost of loading integer registers
390                                            in QImode, HImode and SImode.
391                                            Relative to reg-reg move (2).  */
392   {3, 4, 3},                            /* cost of storing integer registers */
393   4,                                    /* cost of reg,reg fld/fst */
394   {4, 4, 12},                           /* cost of loading fp registers
395                                            in SFmode, DFmode and XFmode */
396   {6, 6, 8},                            /* cost of loading integer registers */
397   2,                                    /* cost of moving MMX register */
398   {3, 3},                               /* cost of loading MMX registers
399                                            in SImode and DImode */
400   {4, 4},                               /* cost of storing MMX registers
401                                            in SImode and DImode */
402   2,                                    /* cost of moving SSE register */
403   {4, 3, 6},                            /* cost of loading SSE registers
404                                            in SImode, DImode and TImode */
405   {4, 4, 5},                            /* cost of storing SSE registers
406                                            in SImode, DImode and TImode */
407   5,                                    /* MMX or SSE register to integer */
408   64,                                   /* size of prefetch block */
409   6,                                    /* number of parallel prefetches */
410   5,                                    /* Branch cost */
411   4,                                    /* cost of FADD and FSUB insns.  */
412   4,                                    /* cost of FMUL instruction.  */
413   19,                                   /* cost of FDIV instruction.  */
414   2,                                    /* cost of FABS instruction.  */
415   2,                                    /* cost of FCHS instruction.  */
416   35,                                   /* cost of FSQRT instruction.  */
417 };
418
419 static const
420 struct processor_costs pentium4_cost = {
421   1,                                    /* cost of an add instruction */
422   3,                                    /* cost of a lea instruction */
423   4,                                    /* variable shift costs */
424   4,                                    /* constant shift costs */
425   {15, 15, 15, 15, 15},                 /* cost of starting a multiply */
426   0,                                    /* cost of multiply per each bit set */
427   {56, 56, 56, 56, 56},                 /* cost of a divide/mod */
428   1,                                    /* cost of movsx */
429   1,                                    /* cost of movzx */
430   16,                                   /* "large" insn */
431   6,                                    /* MOVE_RATIO */
432   2,                                    /* cost for loading QImode using movzbl */
433   {4, 5, 4},                            /* cost of loading integer registers
434                                            in QImode, HImode and SImode.
435                                            Relative to reg-reg move (2).  */
436   {2, 3, 2},                            /* cost of storing integer registers */
437   2,                                    /* cost of reg,reg fld/fst */
438   {2, 2, 6},                            /* cost of loading fp registers
439                                            in SFmode, DFmode and XFmode */
440   {4, 4, 6},                            /* cost of loading integer registers */
441   2,                                    /* cost of moving MMX register */
442   {2, 2},                               /* cost of loading MMX registers
443                                            in SImode and DImode */
444   {2, 2},                               /* cost of storing MMX registers
445                                            in SImode and DImode */
446   12,                                   /* cost of moving SSE register */
447   {12, 12, 12},                         /* cost of loading SSE registers
448                                            in SImode, DImode and TImode */
449   {2, 2, 8},                            /* cost of storing SSE registers
450                                            in SImode, DImode and TImode */
451   10,                                   /* MMX or SSE register to integer */
452   64,                                   /* size of prefetch block */
453   6,                                    /* number of parallel prefetches */
454   2,                                    /* Branch cost */
455   5,                                    /* cost of FADD and FSUB insns.  */
456   7,                                    /* cost of FMUL instruction.  */
457   43,                                   /* cost of FDIV instruction.  */
458   2,                                    /* cost of FABS instruction.  */
459   2,                                    /* cost of FCHS instruction.  */
460   43,                                   /* cost of FSQRT instruction.  */
461 };
462
463 static const
464 struct processor_costs nocona_cost = {
465   1,                                    /* cost of an add instruction */
466   1,                                    /* cost of a lea instruction */
467   1,                                    /* variable shift costs */
468   1,                                    /* constant shift costs */
469   {10, 10, 10, 10, 10},                 /* cost of starting a multiply */
470   0,                                    /* cost of multiply per each bit set */
471   {66, 66, 66, 66, 66},                 /* cost of a divide/mod */
472   1,                                    /* cost of movsx */
473   1,                                    /* cost of movzx */
474   16,                                   /* "large" insn */
475   17,                                   /* MOVE_RATIO */
476   4,                                    /* cost for loading QImode using movzbl */
477   {4, 4, 4},                            /* cost of loading integer registers
478                                            in QImode, HImode and SImode.
479                                            Relative to reg-reg move (2).  */
480   {4, 4, 4},                            /* cost of storing integer registers */
481   3,                                    /* cost of reg,reg fld/fst */
482   {12, 12, 12},                         /* cost of loading fp registers
483                                            in SFmode, DFmode and XFmode */
484   {4, 4, 4},                            /* cost of loading integer registers */
485   6,                                    /* cost of moving MMX register */
486   {12, 12},                             /* cost of loading MMX registers
487                                            in SImode and DImode */
488   {12, 12},                             /* cost of storing MMX registers
489                                            in SImode and DImode */
490   6,                                    /* cost of moving SSE register */
491   {12, 12, 12},                         /* cost of loading SSE registers
492                                            in SImode, DImode and TImode */
493   {12, 12, 12},                         /* cost of storing SSE registers
494                                            in SImode, DImode and TImode */
495   8,                                    /* MMX or SSE register to integer */
496   128,                                  /* size of prefetch block */
497   8,                                    /* number of parallel prefetches */
498   1,                                    /* Branch cost */
499   6,                                    /* cost of FADD and FSUB insns.  */
500   8,                                    /* cost of FMUL instruction.  */
501   40,                                   /* cost of FDIV instruction.  */
502   3,                                    /* cost of FABS instruction.  */
503   3,                                    /* cost of FCHS instruction.  */
504   44,                                   /* cost of FSQRT instruction.  */
505 };
506
507 const struct processor_costs *ix86_cost = &pentium_cost;
508
509 /* Processor feature/optimization bitmasks.  */
510 #define m_386 (1<<PROCESSOR_I386)
511 #define m_486 (1<<PROCESSOR_I486)
512 #define m_PENT (1<<PROCESSOR_PENTIUM)
513 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
514 #define m_K6  (1<<PROCESSOR_K6)
515 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
516 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
517 #define m_K8  (1<<PROCESSOR_K8)
518 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
519 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
520
521 const int x86_use_leave = m_386 | m_K6 | m_ATHLON_K8;
522 const int x86_push_memory = m_386 | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
523 const int x86_zero_extend_with_and = m_486 | m_PENT;
524 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA /* m_386 | m_K6 */;
525 const int x86_double_with_add = ~m_386;
526 const int x86_use_bit_test = m_386;
527 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6;
528 const int x86_cmove = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
529 const int x86_fisttp = m_NOCONA;
530 const int x86_3dnow_a = m_ATHLON_K8;
531 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
532 /* Branch hints were put in P4 based on simulation result. But
533    after P4 was made, no performance benefit was observed with
534    branch hints. It also increases the code size. As the result,
535    icc never generates branch hints.  */
536 const int x86_branch_hints = 0;
537 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4 | m_NOCONA;
538 const int x86_partial_reg_stall = m_PPRO;
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 /* Size of the register save area.  */
737 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
738
739 /* Define the structure for the machine field in struct function.  */
740
741 struct stack_local_entry GTY(())
742 {
743   unsigned short mode;
744   unsigned short n;
745   rtx rtl;
746   struct stack_local_entry *next;
747 };
748
749 /* Structure describing stack frame layout.
750    Stack grows downward:
751
752    [arguments]
753                                               <- ARG_POINTER
754    saved pc
755
756    saved frame pointer if frame_pointer_needed
757                                               <- HARD_FRAME_POINTER
758    [saved regs]
759
760    [padding1]          \
761                         )
762    [va_arg registers]  (
763                         > to_allocate         <- FRAME_POINTER
764    [frame]             (
765                         )
766    [padding2]          /
767   */
768 struct ix86_frame
769 {
770   int nregs;
771   int padding1;
772   int va_arg_size;
773   HOST_WIDE_INT frame;
774   int padding2;
775   int outgoing_arguments_size;
776   int red_zone_size;
777
778   HOST_WIDE_INT to_allocate;
779   /* The offsets relative to ARG_POINTER.  */
780   HOST_WIDE_INT frame_pointer_offset;
781   HOST_WIDE_INT hard_frame_pointer_offset;
782   HOST_WIDE_INT stack_pointer_offset;
783
784   /* When save_regs_using_mov is set, emit prologue using
785      move instead of push instructions.  */
786   bool save_regs_using_mov;
787 };
788
789 /* Code model option.  */
790 enum cmodel ix86_cmodel;
791 /* Asm dialect.  */
792 enum asm_dialect ix86_asm_dialect = ASM_ATT;
793 /* TLS dialext.  */
794 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
795
796 /* Which unit we are generating floating point math for.  */
797 enum fpmath_unit ix86_fpmath;
798
799 /* Which cpu are we scheduling for.  */
800 enum processor_type ix86_tune;
801 /* Which instruction set architecture to use.  */
802 enum processor_type ix86_arch;
803
804 /* true if sse prefetch instruction is not NOOP.  */
805 int x86_prefetch_sse;
806
807 /* ix86_regparm_string as a number */
808 static int ix86_regparm;
809
810 /* Preferred alignment for stack boundary in bits.  */
811 unsigned int ix86_preferred_stack_boundary;
812
813 /* Values 1-5: see jump.c */
814 int ix86_branch_cost;
815
816 /* Variables which are this size or smaller are put in the data/bss
817    or ldata/lbss sections.  */
818
819 int ix86_section_threshold = 65536;
820
821 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
822 char internal_label_prefix[16];
823 int internal_label_prefix_len;
824 \f
825 static bool ix86_handle_option (size_t, const char *, int);
826 static void output_pic_addr_const (FILE *, rtx, int);
827 static void put_condition_code (enum rtx_code, enum machine_mode,
828                                 int, int, FILE *);
829 static const char *get_some_local_dynamic_name (void);
830 static int get_some_local_dynamic_name_1 (rtx *, void *);
831 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
832 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
833                                                    rtx *);
834 static bool ix86_fixed_condition_code_regs (unsigned int *, unsigned int *);
835 static enum machine_mode ix86_cc_modes_compatible (enum machine_mode,
836                                                    enum machine_mode);
837 static rtx get_thread_pointer (int);
838 static rtx legitimize_tls_address (rtx, enum tls_model, int);
839 static void get_pc_thunk_name (char [32], unsigned int);
840 static rtx gen_push (rtx);
841 static int ix86_flags_dependant (rtx, rtx, enum attr_type);
842 static int ix86_agi_dependant (rtx, rtx, enum attr_type);
843 static struct machine_function * ix86_init_machine_status (void);
844 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
845 static int ix86_nsaved_regs (void);
846 static void ix86_emit_save_regs (void);
847 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
848 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
849 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
850 static HOST_WIDE_INT ix86_GOT_alias_set (void);
851 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
852 static rtx ix86_expand_aligntest (rtx, int);
853 static void ix86_expand_strlensi_unroll_1 (rtx, rtx, rtx);
854 static int ix86_issue_rate (void);
855 static int ix86_adjust_cost (rtx, rtx, rtx, int);
856 static int ia32_multipass_dfa_lookahead (void);
857 static void ix86_init_mmx_sse_builtins (void);
858 static rtx x86_this_parameter (tree);
859 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
860                                  HOST_WIDE_INT, tree);
861 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
862 static void x86_file_start (void);
863 static void ix86_reorg (void);
864 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
865 static tree ix86_build_builtin_va_list (void);
866 static void ix86_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
867                                          tree, int *, int);
868 static tree ix86_gimplify_va_arg (tree, tree, tree *, tree *);
869 static bool ix86_vector_mode_supported_p (enum machine_mode);
870
871 static int ix86_address_cost (rtx);
872 static bool ix86_cannot_force_const_mem (rtx);
873 static rtx ix86_delegitimize_address (rtx);
874
875 static void i386_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
876
877 struct builtin_description;
878 static rtx ix86_expand_sse_comi (const struct builtin_description *,
879                                  tree, rtx);
880 static rtx ix86_expand_sse_compare (const struct builtin_description *,
881                                     tree, rtx);
882 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
883 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
884 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
885 static rtx ix86_expand_store_builtin (enum insn_code, tree);
886 static rtx safe_vector_operand (rtx, enum machine_mode);
887 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
888 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
889 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
890 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
891 static int ix86_fp_comparison_cost (enum rtx_code code);
892 static unsigned int ix86_select_alt_pic_regnum (void);
893 static int ix86_save_reg (unsigned int, int);
894 static void ix86_compute_frame_layout (struct ix86_frame *);
895 static int ix86_comp_type_attributes (tree, tree);
896 static int ix86_function_regparm (tree, tree);
897 const struct attribute_spec ix86_attribute_table[];
898 static bool ix86_function_ok_for_sibcall (tree, tree);
899 static tree ix86_handle_cconv_attribute (tree *, tree, tree, int, bool *);
900 static int ix86_value_regno (enum machine_mode, tree, tree);
901 static bool contains_128bit_aligned_vector_p (tree);
902 static rtx ix86_struct_value_rtx (tree, int);
903 static bool ix86_ms_bitfield_layout_p (tree);
904 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
905 static int extended_reg_mentioned_1 (rtx *, void *);
906 static bool ix86_rtx_costs (rtx, int, int, int *);
907 static int min_insn_size (rtx);
908 static tree ix86_md_asm_clobbers (tree outputs, tree inputs, tree clobbers);
909 static bool ix86_must_pass_in_stack (enum machine_mode mode, tree type);
910 static bool ix86_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
911                                     tree, bool);
912 static void ix86_init_builtins (void);
913 static rtx ix86_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
914 static const char *ix86_mangle_fundamental_type (tree);
915 static tree ix86_stack_protect_fail (void);
916 static rtx ix86_internal_arg_pointer (void);
917 static void ix86_dwarf_handle_frame_unspec (const char *, rtx, int);
918
919 /* This function is only used on Solaris.  */
920 static void i386_solaris_elf_named_section (const char *, unsigned int, tree)
921   ATTRIBUTE_UNUSED;
922
923 /* Register class used for passing given 64bit part of the argument.
924    These represent classes as documented by the PS ABI, with the exception
925    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
926    use SF or DFmode move instead of DImode to avoid reformatting penalties.
927
928    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
929    whenever possible (upper half does contain padding).
930  */
931 enum x86_64_reg_class
932   {
933     X86_64_NO_CLASS,
934     X86_64_INTEGER_CLASS,
935     X86_64_INTEGERSI_CLASS,
936     X86_64_SSE_CLASS,
937     X86_64_SSESF_CLASS,
938     X86_64_SSEDF_CLASS,
939     X86_64_SSEUP_CLASS,
940     X86_64_X87_CLASS,
941     X86_64_X87UP_CLASS,
942     X86_64_COMPLEX_X87_CLASS,
943     X86_64_MEMORY_CLASS
944   };
945 static const char * const x86_64_reg_class_name[] = {
946   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
947   "sseup", "x87", "x87up", "cplx87", "no"
948 };
949
950 #define MAX_CLASSES 4
951
952 /* Table of constants used by fldpi, fldln2, etc....  */
953 static REAL_VALUE_TYPE ext_80387_constants_table [5];
954 static bool ext_80387_constants_init = 0;
955 static void init_ext_80387_constants (void);
956 static bool ix86_in_large_data_p (tree) ATTRIBUTE_UNUSED;
957 static void ix86_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
958 static void x86_64_elf_unique_section (tree decl, int reloc) ATTRIBUTE_UNUSED;
959 static section *x86_64_elf_select_section (tree decl, int reloc,
960                                            unsigned HOST_WIDE_INT align)
961                                              ATTRIBUTE_UNUSED;
962 \f
963 /* Initialize the GCC target structure.  */
964 #undef TARGET_ATTRIBUTE_TABLE
965 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
966 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
967 #  undef TARGET_MERGE_DECL_ATTRIBUTES
968 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
969 #endif
970
971 #undef TARGET_COMP_TYPE_ATTRIBUTES
972 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
973
974 #undef TARGET_INIT_BUILTINS
975 #define TARGET_INIT_BUILTINS ix86_init_builtins
976 #undef TARGET_EXPAND_BUILTIN
977 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
978
979 #undef TARGET_ASM_FUNCTION_EPILOGUE
980 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
981
982 #undef TARGET_ENCODE_SECTION_INFO
983 #ifndef SUBTARGET_ENCODE_SECTION_INFO
984 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
985 #else
986 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
987 #endif
988
989 #undef TARGET_ASM_OPEN_PAREN
990 #define TARGET_ASM_OPEN_PAREN ""
991 #undef TARGET_ASM_CLOSE_PAREN
992 #define TARGET_ASM_CLOSE_PAREN ""
993
994 #undef TARGET_ASM_ALIGNED_HI_OP
995 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
996 #undef TARGET_ASM_ALIGNED_SI_OP
997 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
998 #ifdef ASM_QUAD
999 #undef TARGET_ASM_ALIGNED_DI_OP
1000 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
1001 #endif
1002
1003 #undef TARGET_ASM_UNALIGNED_HI_OP
1004 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
1005 #undef TARGET_ASM_UNALIGNED_SI_OP
1006 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
1007 #undef TARGET_ASM_UNALIGNED_DI_OP
1008 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
1009
1010 #undef TARGET_SCHED_ADJUST_COST
1011 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
1012 #undef TARGET_SCHED_ISSUE_RATE
1013 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
1014 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1015 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1016   ia32_multipass_dfa_lookahead
1017
1018 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1019 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
1020
1021 #ifdef HAVE_AS_TLS
1022 #undef TARGET_HAVE_TLS
1023 #define TARGET_HAVE_TLS true
1024 #endif
1025 #undef TARGET_CANNOT_FORCE_CONST_MEM
1026 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
1027
1028 #undef TARGET_DELEGITIMIZE_ADDRESS
1029 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
1030
1031 #undef TARGET_MS_BITFIELD_LAYOUT_P
1032 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
1033
1034 #if TARGET_MACHO
1035 #undef TARGET_BINDS_LOCAL_P
1036 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1037 #endif
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 #undef TARGET_INTERNAL_ARG_POINTER
1087 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
1088 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
1089 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
1090
1091 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1092 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
1093
1094 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1095 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
1096
1097 #ifdef HAVE_AS_TLS
1098 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1099 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
1100 #endif
1101
1102 #ifdef SUBTARGET_INSERT_ATTRIBUTES
1103 #undef TARGET_INSERT_ATTRIBUTES
1104 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
1105 #endif
1106
1107 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
1108 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
1109
1110 #undef TARGET_STACK_PROTECT_FAIL
1111 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
1112
1113 #undef TARGET_FUNCTION_VALUE
1114 #define TARGET_FUNCTION_VALUE ix86_function_value
1115
1116 struct gcc_target targetm = TARGET_INITIALIZER;
1117
1118 \f
1119 /* The svr4 ABI for the i386 says that records and unions are returned
1120    in memory.  */
1121 #ifndef DEFAULT_PCC_STRUCT_RETURN
1122 #define DEFAULT_PCC_STRUCT_RETURN 1
1123 #endif
1124
1125 /* Implement TARGET_HANDLE_OPTION.  */
1126
1127 static bool
1128 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1129 {
1130   switch (code)
1131     {
1132     case OPT_m3dnow:
1133       if (!value)
1134         {
1135           target_flags &= ~MASK_3DNOW_A;
1136           target_flags_explicit |= MASK_3DNOW_A;
1137         }
1138       return true;
1139
1140     case OPT_mmmx:
1141       if (!value)
1142         {
1143           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1144           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1145         }
1146       return true;
1147
1148     case OPT_msse:
1149       if (!value)
1150         {
1151           target_flags &= ~(MASK_SSE2 | MASK_SSE3);
1152           target_flags_explicit |= MASK_SSE2 | MASK_SSE3;
1153         }
1154       return true;
1155
1156     case OPT_msse2:
1157       if (!value)
1158         {
1159           target_flags &= ~MASK_SSE3;
1160           target_flags_explicit |= MASK_SSE3;
1161         }
1162       return true;
1163
1164     default:
1165       return true;
1166     }
1167 }
1168
1169 /* Sometimes certain combinations of command options do not make
1170    sense on a particular target machine.  You can define a macro
1171    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1172    defined, is executed once just after all the command options have
1173    been parsed.
1174
1175    Don't use this macro to turn on various extra optimizations for
1176    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1177
1178 void
1179 override_options (void)
1180 {
1181   int i;
1182   int ix86_tune_defaulted = 0;
1183
1184   /* Comes from final.c -- no real reason to change it.  */
1185 #define MAX_CODE_ALIGN 16
1186
1187   static struct ptt
1188     {
1189       const struct processor_costs *cost;       /* Processor costs */
1190       const int target_enable;                  /* Target flags to enable.  */
1191       const int target_disable;                 /* Target flags to disable.  */
1192       const int align_loop;                     /* Default alignments.  */
1193       const int align_loop_max_skip;
1194       const int align_jump;
1195       const int align_jump_max_skip;
1196       const int align_func;
1197     }
1198   const processor_target_table[PROCESSOR_max] =
1199     {
1200       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1201       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1202       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1203       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1204       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1205       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1206       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1207       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1208       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0}
1209     };
1210
1211   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1212   static struct pta
1213     {
1214       const char *const name;           /* processor name or nickname.  */
1215       const enum processor_type processor;
1216       const enum pta_flags
1217         {
1218           PTA_SSE = 1,
1219           PTA_SSE2 = 2,
1220           PTA_SSE3 = 4,
1221           PTA_MMX = 8,
1222           PTA_PREFETCH_SSE = 16,
1223           PTA_3DNOW = 32,
1224           PTA_3DNOW_A = 64,
1225           PTA_64BIT = 128
1226         } flags;
1227     }
1228   const processor_alias_table[] =
1229     {
1230       {"i386", PROCESSOR_I386, 0},
1231       {"i486", PROCESSOR_I486, 0},
1232       {"i586", PROCESSOR_PENTIUM, 0},
1233       {"pentium", PROCESSOR_PENTIUM, 0},
1234       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1235       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1236       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1237       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1238       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1239       {"i686", PROCESSOR_PENTIUMPRO, 0},
1240       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1241       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1242       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1243       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1244       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1245       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1246                                        | PTA_MMX | PTA_PREFETCH_SSE},
1247       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1248                                         | PTA_MMX | PTA_PREFETCH_SSE},
1249       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1250                                         | PTA_MMX | PTA_PREFETCH_SSE},
1251       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1252                                         | PTA_MMX | PTA_PREFETCH_SSE},
1253       {"k6", PROCESSOR_K6, PTA_MMX},
1254       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1255       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1256       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1257                                    | PTA_3DNOW_A},
1258       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1259                                          | PTA_3DNOW | PTA_3DNOW_A},
1260       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1261                                     | PTA_3DNOW_A | PTA_SSE},
1262       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1263                                       | PTA_3DNOW_A | PTA_SSE},
1264       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1265                                       | PTA_3DNOW_A | PTA_SSE},
1266       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1267                                | PTA_SSE | PTA_SSE2 },
1268       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1269                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1270       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1271                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1272       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1273                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1274       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1275                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1276     };
1277
1278   int const pta_size = ARRAY_SIZE (processor_alias_table);
1279
1280 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1281   SUBTARGET_OVERRIDE_OPTIONS;
1282 #endif
1283
1284   /* Set the default values for switches whose default depends on TARGET_64BIT
1285      in case they weren't overwritten by command line options.  */
1286   if (TARGET_64BIT)
1287     {
1288       if (flag_omit_frame_pointer == 2)
1289         flag_omit_frame_pointer = 1;
1290       if (flag_asynchronous_unwind_tables == 2)
1291         flag_asynchronous_unwind_tables = 1;
1292       if (flag_pcc_struct_return == 2)
1293         flag_pcc_struct_return = 0;
1294     }
1295   else
1296     {
1297       if (flag_omit_frame_pointer == 2)
1298         flag_omit_frame_pointer = 0;
1299       if (flag_asynchronous_unwind_tables == 2)
1300         flag_asynchronous_unwind_tables = 0;
1301       if (flag_pcc_struct_return == 2)
1302         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1303     }
1304
1305   if (!ix86_tune_string && ix86_arch_string)
1306     ix86_tune_string = ix86_arch_string;
1307   if (!ix86_tune_string)
1308     {
1309       ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1310       ix86_tune_defaulted = 1;
1311     }
1312   if (!ix86_arch_string)
1313     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1314
1315   if (ix86_cmodel_string != 0)
1316     {
1317       if (!strcmp (ix86_cmodel_string, "small"))
1318         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1319       else if (!strcmp (ix86_cmodel_string, "medium"))
1320         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1321       else if (flag_pic)
1322         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1323       else if (!strcmp (ix86_cmodel_string, "32"))
1324         ix86_cmodel = CM_32;
1325       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1326         ix86_cmodel = CM_KERNEL;
1327       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1328         ix86_cmodel = CM_LARGE;
1329       else
1330         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1331     }
1332   else
1333     {
1334       ix86_cmodel = CM_32;
1335       if (TARGET_64BIT)
1336         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1337     }
1338   if (ix86_asm_string != 0)
1339     {
1340       if (! TARGET_MACHO
1341           && !strcmp (ix86_asm_string, "intel"))
1342         ix86_asm_dialect = ASM_INTEL;
1343       else if (!strcmp (ix86_asm_string, "att"))
1344         ix86_asm_dialect = ASM_ATT;
1345       else
1346         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1347     }
1348   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1349     error ("code model %qs not supported in the %s bit mode",
1350            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1351   if (ix86_cmodel == CM_LARGE)
1352     sorry ("code model %<large%> not supported yet");
1353   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1354     sorry ("%i-bit mode not compiled in",
1355            (target_flags & MASK_64BIT) ? 64 : 32);
1356
1357   for (i = 0; i < pta_size; i++)
1358     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1359       {
1360         ix86_arch = processor_alias_table[i].processor;
1361         /* Default cpu tuning to the architecture.  */
1362         ix86_tune = ix86_arch;
1363         if (processor_alias_table[i].flags & PTA_MMX
1364             && !(target_flags_explicit & MASK_MMX))
1365           target_flags |= MASK_MMX;
1366         if (processor_alias_table[i].flags & PTA_3DNOW
1367             && !(target_flags_explicit & MASK_3DNOW))
1368           target_flags |= MASK_3DNOW;
1369         if (processor_alias_table[i].flags & PTA_3DNOW_A
1370             && !(target_flags_explicit & MASK_3DNOW_A))
1371           target_flags |= MASK_3DNOW_A;
1372         if (processor_alias_table[i].flags & PTA_SSE
1373             && !(target_flags_explicit & MASK_SSE))
1374           target_flags |= MASK_SSE;
1375         if (processor_alias_table[i].flags & PTA_SSE2
1376             && !(target_flags_explicit & MASK_SSE2))
1377           target_flags |= MASK_SSE2;
1378         if (processor_alias_table[i].flags & PTA_SSE3
1379             && !(target_flags_explicit & MASK_SSE3))
1380           target_flags |= MASK_SSE3;
1381         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1382           x86_prefetch_sse = true;
1383         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1384           error ("CPU you selected does not support x86-64 "
1385                  "instruction set");
1386         break;
1387       }
1388
1389   if (i == pta_size)
1390     error ("bad value (%s) for -march= switch", ix86_arch_string);
1391
1392   for (i = 0; i < pta_size; i++)
1393     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1394       {
1395         ix86_tune = processor_alias_table[i].processor;
1396         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1397           {
1398             if (ix86_tune_defaulted)
1399               {
1400                 ix86_tune_string = "x86-64";
1401                 for (i = 0; i < pta_size; i++)
1402                   if (! strcmp (ix86_tune_string,
1403                                 processor_alias_table[i].name))
1404                     break;
1405                 ix86_tune = processor_alias_table[i].processor;
1406               }
1407             else
1408               error ("CPU you selected does not support x86-64 "
1409                      "instruction set");
1410           }
1411         /* Intel CPUs have always interpreted SSE prefetch instructions as
1412            NOPs; so, we can enable SSE prefetch instructions even when
1413            -mtune (rather than -march) points us to a processor that has them.
1414            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1415            higher processors.  */
1416         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1417           x86_prefetch_sse = true;
1418         break;
1419       }
1420   if (i == pta_size)
1421     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1422
1423   if (optimize_size)
1424     ix86_cost = &size_cost;
1425   else
1426     ix86_cost = processor_target_table[ix86_tune].cost;
1427   target_flags |= processor_target_table[ix86_tune].target_enable;
1428   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1429
1430   /* Arrange to set up i386_stack_locals for all functions.  */
1431   init_machine_status = ix86_init_machine_status;
1432
1433   /* Validate -mregparm= value.  */
1434   if (ix86_regparm_string)
1435     {
1436       i = atoi (ix86_regparm_string);
1437       if (i < 0 || i > REGPARM_MAX)
1438         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1439       else
1440         ix86_regparm = i;
1441     }
1442   else
1443    if (TARGET_64BIT)
1444      ix86_regparm = REGPARM_MAX;
1445
1446   /* If the user has provided any of the -malign-* options,
1447      warn and use that value only if -falign-* is not set.
1448      Remove this code in GCC 3.2 or later.  */
1449   if (ix86_align_loops_string)
1450     {
1451       warning (0, "-malign-loops is obsolete, use -falign-loops");
1452       if (align_loops == 0)
1453         {
1454           i = atoi (ix86_align_loops_string);
1455           if (i < 0 || i > MAX_CODE_ALIGN)
1456             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1457           else
1458             align_loops = 1 << i;
1459         }
1460     }
1461
1462   if (ix86_align_jumps_string)
1463     {
1464       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
1465       if (align_jumps == 0)
1466         {
1467           i = atoi (ix86_align_jumps_string);
1468           if (i < 0 || i > MAX_CODE_ALIGN)
1469             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1470           else
1471             align_jumps = 1 << i;
1472         }
1473     }
1474
1475   if (ix86_align_funcs_string)
1476     {
1477       warning (0, "-malign-functions is obsolete, use -falign-functions");
1478       if (align_functions == 0)
1479         {
1480           i = atoi (ix86_align_funcs_string);
1481           if (i < 0 || i > MAX_CODE_ALIGN)
1482             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1483           else
1484             align_functions = 1 << i;
1485         }
1486     }
1487
1488   /* Default align_* from the processor table.  */
1489   if (align_loops == 0)
1490     {
1491       align_loops = processor_target_table[ix86_tune].align_loop;
1492       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
1493     }
1494   if (align_jumps == 0)
1495     {
1496       align_jumps = processor_target_table[ix86_tune].align_jump;
1497       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
1498     }
1499   if (align_functions == 0)
1500     {
1501       align_functions = processor_target_table[ix86_tune].align_func;
1502     }
1503
1504   /* Validate -mpreferred-stack-boundary= value, or provide default.
1505      The default of 128 bits is for Pentium III's SSE __m128, but we
1506      don't want additional code to keep the stack aligned when
1507      optimizing for code size.  */
1508   ix86_preferred_stack_boundary = (optimize_size
1509                                    ? TARGET_64BIT ? 128 : 32
1510                                    : 128);
1511   if (ix86_preferred_stack_boundary_string)
1512     {
1513       i = atoi (ix86_preferred_stack_boundary_string);
1514       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1515         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1516                TARGET_64BIT ? 4 : 2);
1517       else
1518         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1519     }
1520
1521   /* Validate -mbranch-cost= value, or provide default.  */
1522   ix86_branch_cost = processor_target_table[ix86_tune].cost->branch_cost;
1523   if (ix86_branch_cost_string)
1524     {
1525       i = atoi (ix86_branch_cost_string);
1526       if (i < 0 || i > 5)
1527         error ("-mbranch-cost=%d is not between 0 and 5", i);
1528       else
1529         ix86_branch_cost = i;
1530     }
1531   if (ix86_section_threshold_string)
1532     {
1533       i = atoi (ix86_section_threshold_string);
1534       if (i < 0)
1535         error ("-mlarge-data-threshold=%d is negative", i);
1536       else
1537         ix86_section_threshold = i;
1538     }
1539
1540   if (ix86_tls_dialect_string)
1541     {
1542       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1543         ix86_tls_dialect = TLS_DIALECT_GNU;
1544       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1545         ix86_tls_dialect = TLS_DIALECT_SUN;
1546       else
1547         error ("bad value (%s) for -mtls-dialect= switch",
1548                ix86_tls_dialect_string);
1549     }
1550
1551   /* Keep nonleaf frame pointers.  */
1552   if (flag_omit_frame_pointer)
1553     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
1554   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
1555     flag_omit_frame_pointer = 1;
1556
1557   /* If we're doing fast math, we don't care about comparison order
1558      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1559   if (flag_unsafe_math_optimizations)
1560     target_flags &= ~MASK_IEEE_FP;
1561
1562   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1563      since the insns won't need emulation.  */
1564   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1565     target_flags &= ~MASK_NO_FANCY_MATH_387;
1566
1567   /* Likewise, if the target doesn't have a 387, or we've specified
1568      software floating point, don't use 387 inline intrinsics.  */
1569   if (!TARGET_80387)
1570     target_flags |= MASK_NO_FANCY_MATH_387;
1571
1572   /* Turn on SSE2 builtins for -msse3.  */
1573   if (TARGET_SSE3)
1574     target_flags |= MASK_SSE2;
1575
1576   /* Turn on SSE builtins for -msse2.  */
1577   if (TARGET_SSE2)
1578     target_flags |= MASK_SSE;
1579
1580   /* Turn on MMX builtins for -msse.  */
1581   if (TARGET_SSE)
1582     {
1583       target_flags |= MASK_MMX & ~target_flags_explicit;
1584       x86_prefetch_sse = true;
1585     }
1586
1587   /* Turn on MMX builtins for 3Dnow.  */
1588   if (TARGET_3DNOW)
1589     target_flags |= MASK_MMX;
1590
1591   if (TARGET_64BIT)
1592     {
1593       if (TARGET_ALIGN_DOUBLE)
1594         error ("-malign-double makes no sense in the 64bit mode");
1595       if (TARGET_RTD)
1596         error ("-mrtd calling convention not supported in the 64bit mode");
1597
1598       /* Enable by default the SSE and MMX builtins.  Do allow the user to
1599          explicitly disable any of these.  In particular, disabling SSE and
1600          MMX for kernel code is extremely useful.  */
1601       target_flags
1602         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE)
1603             & ~target_flags_explicit);
1604      }
1605   else
1606     {
1607       /* i386 ABI does not specify red zone.  It still makes sense to use it
1608          when programmer takes care to stack from being destroyed.  */
1609       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
1610         target_flags |= MASK_NO_RED_ZONE;
1611     }
1612
1613   /* Accept -msseregparm only if at least SSE support is enabled.  */
1614   if (TARGET_SSEREGPARM
1615       && ! TARGET_SSE)
1616     error ("-msseregparm used without SSE enabled");
1617
1618   ix86_fpmath = TARGET_FPMATH_DEFAULT;
1619
1620   if (ix86_fpmath_string != 0)
1621     {
1622       if (! strcmp (ix86_fpmath_string, "387"))
1623         ix86_fpmath = FPMATH_387;
1624       else if (! strcmp (ix86_fpmath_string, "sse"))
1625         {
1626           if (!TARGET_SSE)
1627             {
1628               warning (0, "SSE instruction set disabled, using 387 arithmetics");
1629               ix86_fpmath = FPMATH_387;
1630             }
1631           else
1632             ix86_fpmath = FPMATH_SSE;
1633         }
1634       else if (! strcmp (ix86_fpmath_string, "387,sse")
1635                || ! strcmp (ix86_fpmath_string, "sse,387"))
1636         {
1637           if (!TARGET_SSE)
1638             {
1639               warning (0, "SSE instruction set disabled, using 387 arithmetics");
1640               ix86_fpmath = FPMATH_387;
1641             }
1642           else if (!TARGET_80387)
1643             {
1644               warning (0, "387 instruction set disabled, using SSE arithmetics");
1645               ix86_fpmath = FPMATH_SSE;
1646             }
1647           else
1648             ix86_fpmath = FPMATH_SSE | FPMATH_387;
1649         }
1650       else
1651         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
1652     }
1653
1654   /* If the i387 is disabled, then do not return values in it. */
1655   if (!TARGET_80387)
1656     target_flags &= ~MASK_FLOAT_RETURNS;
1657
1658   if ((x86_accumulate_outgoing_args & TUNEMASK)
1659       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1660       && !optimize_size)
1661     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1662
1663   /* ??? Unwind info is not correct around the CFG unless either a frame
1664      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
1665      unwind info generation to be aware of the CFG and propagating states
1666      around edges.  */
1667   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
1668        || flag_exceptions || flag_non_call_exceptions)
1669       && flag_omit_frame_pointer
1670       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
1671     {
1672       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1673         warning (0, "unwind tables currently require either a frame pointer "
1674                  "or -maccumulate-outgoing-args for correctness");
1675       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1676     }
1677
1678   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1679   {
1680     char *p;
1681     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1682     p = strchr (internal_label_prefix, 'X');
1683     internal_label_prefix_len = p - internal_label_prefix;
1684     *p = '\0';
1685   }
1686
1687   /* When scheduling description is not available, disable scheduler pass
1688      so it won't slow down the compilation and make x87 code slower.  */
1689   if (!TARGET_SCHEDULE)
1690     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
1691 }
1692 \f
1693 /* switch to the appropriate section for output of DECL.
1694    DECL is either a `VAR_DECL' node or a constant of some sort.
1695    RELOC indicates whether forming the initial value of DECL requires
1696    link-time relocations.  */
1697
1698 static section *
1699 x86_64_elf_select_section (tree decl, int reloc,
1700                            unsigned HOST_WIDE_INT align)
1701 {
1702   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
1703       && ix86_in_large_data_p (decl))
1704     {
1705       const char *sname = NULL;
1706       unsigned int flags = SECTION_WRITE;
1707       switch (categorize_decl_for_section (decl, reloc, flag_pic))
1708         {
1709         case SECCAT_DATA:
1710           sname = ".ldata";
1711           break;
1712         case SECCAT_DATA_REL:
1713           sname = ".ldata.rel";
1714           break;
1715         case SECCAT_DATA_REL_LOCAL:
1716           sname = ".ldata.rel.local";
1717           break;
1718         case SECCAT_DATA_REL_RO:
1719           sname = ".ldata.rel.ro";
1720           break;
1721         case SECCAT_DATA_REL_RO_LOCAL:
1722           sname = ".ldata.rel.ro.local";
1723           break;
1724         case SECCAT_BSS:
1725           sname = ".lbss";
1726           flags |= SECTION_BSS;
1727           break;
1728         case SECCAT_RODATA:
1729         case SECCAT_RODATA_MERGE_STR:
1730         case SECCAT_RODATA_MERGE_STR_INIT:
1731         case SECCAT_RODATA_MERGE_CONST:
1732           sname = ".lrodata";
1733           flags = 0;
1734           break;
1735         case SECCAT_SRODATA:
1736         case SECCAT_SDATA:
1737         case SECCAT_SBSS:
1738           gcc_unreachable ();
1739         case SECCAT_TEXT:
1740         case SECCAT_TDATA:
1741         case SECCAT_TBSS:
1742           /* We don't split these for medium model.  Place them into
1743              default sections and hope for best.  */
1744           break;
1745         }
1746       if (sname)
1747         {
1748           /* We might get called with string constants, but get_named_section
1749              doesn't like them as they are not DECLs.  Also, we need to set
1750              flags in that case.  */
1751           if (!DECL_P (decl))
1752             return get_section (sname, flags, NULL);
1753           return get_named_section (decl, sname, reloc);
1754         }
1755     }
1756   return default_elf_select_section (decl, reloc, align);
1757 }
1758
1759 /* Build up a unique section name, expressed as a
1760    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
1761    RELOC indicates whether the initial value of EXP requires
1762    link-time relocations.  */
1763
1764 static void
1765 x86_64_elf_unique_section (tree decl, int reloc)
1766 {
1767   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
1768       && ix86_in_large_data_p (decl))
1769     {
1770       const char *prefix = NULL;
1771       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
1772       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
1773
1774       switch (categorize_decl_for_section (decl, reloc, flag_pic))
1775         {
1776         case SECCAT_DATA:
1777         case SECCAT_DATA_REL:
1778         case SECCAT_DATA_REL_LOCAL:
1779         case SECCAT_DATA_REL_RO:
1780         case SECCAT_DATA_REL_RO_LOCAL:
1781           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
1782           break;
1783         case SECCAT_BSS:
1784           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
1785           break;
1786         case SECCAT_RODATA:
1787         case SECCAT_RODATA_MERGE_STR:
1788         case SECCAT_RODATA_MERGE_STR_INIT:
1789         case SECCAT_RODATA_MERGE_CONST:
1790           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
1791           break;
1792         case SECCAT_SRODATA:
1793         case SECCAT_SDATA:
1794         case SECCAT_SBSS:
1795           gcc_unreachable ();
1796         case SECCAT_TEXT:
1797         case SECCAT_TDATA:
1798         case SECCAT_TBSS:
1799           /* We don't split these for medium model.  Place them into
1800              default sections and hope for best.  */
1801           break;
1802         }
1803       if (prefix)
1804         {
1805           const char *name;
1806           size_t nlen, plen;
1807           char *string;
1808           plen = strlen (prefix);
1809
1810           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1811           name = targetm.strip_name_encoding (name);
1812           nlen = strlen (name);
1813
1814           string = alloca (nlen + plen + 1);
1815           memcpy (string, prefix, plen);
1816           memcpy (string + plen, name, nlen + 1);
1817
1818           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
1819           return;
1820         }
1821     }
1822   default_unique_section (decl, reloc);
1823 }
1824
1825 #ifdef COMMON_ASM_OP
1826 /* This says how to output assembler code to declare an
1827    uninitialized external linkage data object.
1828
1829    For medium model x86-64 we need to use .largecomm opcode for
1830    large objects.  */
1831 void
1832 x86_elf_aligned_common (FILE *file,
1833                         const char *name, unsigned HOST_WIDE_INT size,
1834                         int align)
1835 {
1836   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
1837       && size > (unsigned int)ix86_section_threshold)
1838     fprintf (file, ".largecomm\t");
1839   else
1840     fprintf (file, "%s", COMMON_ASM_OP);
1841   assemble_name (file, name);
1842   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
1843            size, align / BITS_PER_UNIT);
1844 }
1845
1846 /* Utility function for targets to use in implementing
1847    ASM_OUTPUT_ALIGNED_BSS.  */
1848
1849 void
1850 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
1851                         const char *name, unsigned HOST_WIDE_INT size,
1852                         int align)
1853 {
1854   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
1855       && size > (unsigned int)ix86_section_threshold)
1856     switch_to_section (get_named_section (decl, ".lbss", 0));
1857   else
1858     switch_to_section (bss_section);
1859   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
1860 #ifdef ASM_DECLARE_OBJECT_NAME
1861   last_assemble_variable_decl = decl;
1862   ASM_DECLARE_OBJECT_NAME (file, name, decl);
1863 #else
1864   /* Standard thing is just output label for the object.  */
1865   ASM_OUTPUT_LABEL (file, name);
1866 #endif /* ASM_DECLARE_OBJECT_NAME */
1867   ASM_OUTPUT_SKIP (file, size ? size : 1);
1868 }
1869 #endif
1870 \f
1871 void
1872 optimization_options (int level, int size ATTRIBUTE_UNUSED)
1873 {
1874   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
1875      make the problem with not enough registers even worse.  */
1876 #ifdef INSN_SCHEDULING
1877   if (level > 1)
1878     flag_schedule_insns = 0;
1879 #endif
1880
1881   if (TARGET_MACHO)
1882     /* The Darwin libraries never set errno, so we might as well
1883        avoid calling them when that's the only reason we would.  */
1884     flag_errno_math = 0;
1885
1886   /* The default values of these switches depend on the TARGET_64BIT
1887      that is not known at this moment.  Mark these values with 2 and
1888      let user the to override these.  In case there is no command line option
1889      specifying them, we will set the defaults in override_options.  */
1890   if (optimize >= 1)
1891     flag_omit_frame_pointer = 2;
1892   flag_pcc_struct_return = 2;
1893   flag_asynchronous_unwind_tables = 2;
1894 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
1895   SUBTARGET_OPTIMIZATION_OPTIONS;
1896 #endif
1897 }
1898 \f
1899 /* Table of valid machine attributes.  */
1900 const struct attribute_spec ix86_attribute_table[] =
1901 {
1902   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1903   /* Stdcall attribute says callee is responsible for popping arguments
1904      if they are not variable.  */
1905   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
1906   /* Fastcall attribute says callee is responsible for popping arguments
1907      if they are not variable.  */
1908   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
1909   /* Cdecl attribute says the callee is a normal C declaration */
1910   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
1911   /* Regparm attribute specifies how many integer arguments are to be
1912      passed in registers.  */
1913   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
1914   /* Sseregparm attribute says we are using x86_64 calling conventions
1915      for FP arguments.  */
1916   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
1917 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1918   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
1919   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
1920   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
1921 #endif
1922   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1923   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1924 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1925   SUBTARGET_ATTRIBUTE_TABLE,
1926 #endif
1927   { NULL,        0, 0, false, false, false, NULL }
1928 };
1929
1930 /* Decide whether we can make a sibling call to a function.  DECL is the
1931    declaration of the function being targeted by the call and EXP is the
1932    CALL_EXPR representing the call.  */
1933
1934 static bool
1935 ix86_function_ok_for_sibcall (tree decl, tree exp)
1936 {
1937   tree func;
1938   rtx a, b;
1939
1940   /* If we are generating position-independent code, we cannot sibcall
1941      optimize any indirect call, or a direct call to a global function,
1942      as the PLT requires %ebx be live.  */
1943   if (!TARGET_64BIT && flag_pic && (!decl || TREE_PUBLIC (decl)))
1944     return false;
1945
1946   if (decl)
1947     func = decl;
1948   else
1949     {
1950       func = TREE_TYPE (TREE_OPERAND (exp, 0));
1951       if (POINTER_TYPE_P (func))
1952         func = TREE_TYPE (func);
1953     }
1954
1955   /* Check that the return value locations are the same.  Like
1956      if we are returning floats on the 80387 register stack, we cannot
1957      make a sibcall from a function that doesn't return a float to a
1958      function that does or, conversely, from a function that does return
1959      a float to a function that doesn't; the necessary stack adjustment
1960      would not be executed.  This is also the place we notice
1961      differences in the return value ABI.  Note that it is ok for one
1962      of the functions to have void return type as long as the return
1963      value of the other is passed in a register.  */
1964   a = ix86_function_value (TREE_TYPE (exp), func, false);
1965   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
1966                            cfun->decl, false);
1967   if (STACK_REG_P (a) || STACK_REG_P (b))
1968     {
1969       if (!rtx_equal_p (a, b))
1970         return false;
1971     }
1972   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
1973     ;
1974   else if (!rtx_equal_p (a, b))
1975     return false;
1976
1977   /* If this call is indirect, we'll need to be able to use a call-clobbered
1978      register for the address of the target function.  Make sure that all
1979      such registers are not used for passing parameters.  */
1980   if (!decl && !TARGET_64BIT)
1981     {
1982       tree type;
1983
1984       /* We're looking at the CALL_EXPR, we need the type of the function.  */
1985       type = TREE_OPERAND (exp, 0);             /* pointer expression */
1986       type = TREE_TYPE (type);                  /* pointer type */
1987       type = TREE_TYPE (type);                  /* function type */
1988
1989       if (ix86_function_regparm (type, NULL) >= 3)
1990         {
1991           /* ??? Need to count the actual number of registers to be used,
1992              not the possible number of registers.  Fix later.  */
1993           return false;
1994         }
1995     }
1996
1997 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1998   /* Dllimport'd functions are also called indirectly.  */
1999   if (decl && DECL_DLLIMPORT_P (decl)
2000       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2001     return false;
2002 #endif
2003
2004   /* If we forced aligned the stack, then sibcalling would unalign the
2005      stack, which may break the called function.  */
2006   if (cfun->machine->force_align_arg_pointer)
2007     return false;
2008
2009   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2010   return true;
2011 }
2012
2013 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
2014    calling convention attributes;
2015    arguments as in struct attribute_spec.handler.  */
2016
2017 static tree
2018 ix86_handle_cconv_attribute (tree *node, tree name,
2019                                    tree args,
2020                                    int flags ATTRIBUTE_UNUSED,
2021                                    bool *no_add_attrs)
2022 {
2023   if (TREE_CODE (*node) != FUNCTION_TYPE
2024       && TREE_CODE (*node) != METHOD_TYPE
2025       && TREE_CODE (*node) != FIELD_DECL
2026       && TREE_CODE (*node) != TYPE_DECL)
2027     {
2028       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2029                IDENTIFIER_POINTER (name));
2030       *no_add_attrs = true;
2031       return NULL_TREE;
2032     }
2033
2034   /* Can combine regparm with all attributes but fastcall.  */
2035   if (is_attribute_p ("regparm", name))
2036     {
2037       tree cst;
2038
2039       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2040         {
2041           error ("fastcall and regparm attributes are not compatible");
2042         }
2043
2044       cst = TREE_VALUE (args);
2045       if (TREE_CODE (cst) != INTEGER_CST)
2046         {
2047           warning (OPT_Wattributes,
2048                    "%qs attribute requires an integer constant argument",
2049                    IDENTIFIER_POINTER (name));
2050           *no_add_attrs = true;
2051         }
2052       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2053         {
2054           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2055                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2056           *no_add_attrs = true;
2057         }
2058
2059       return NULL_TREE;
2060     }
2061
2062   if (TARGET_64BIT)
2063     {
2064       warning (OPT_Wattributes, "%qs attribute ignored",
2065                IDENTIFIER_POINTER (name));
2066       *no_add_attrs = true;
2067       return NULL_TREE;
2068     }
2069
2070   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
2071   if (is_attribute_p ("fastcall", name))
2072     {
2073       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2074         {
2075           error ("fastcall and cdecl attributes are not compatible");
2076         }
2077       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2078         {
2079           error ("fastcall and stdcall attributes are not compatible");
2080         }
2081       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2082         {
2083           error ("fastcall and regparm attributes are not compatible");
2084         }
2085     }
2086
2087   /* Can combine stdcall with fastcall (redundant), regparm and
2088      sseregparm.  */
2089   else if (is_attribute_p ("stdcall", name))
2090     {
2091       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2092         {
2093           error ("stdcall and cdecl attributes are not compatible");
2094         }
2095       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2096         {
2097           error ("stdcall and fastcall attributes are not compatible");
2098         }
2099     }
2100
2101   /* Can combine cdecl with regparm and sseregparm.  */
2102   else if (is_attribute_p ("cdecl", name))
2103     {
2104       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2105         {
2106           error ("stdcall and cdecl attributes are not compatible");
2107         }
2108       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2109         {
2110           error ("fastcall and cdecl attributes are not compatible");
2111         }
2112     }
2113
2114   /* Can combine sseregparm with all attributes.  */
2115
2116   return NULL_TREE;
2117 }
2118
2119 /* Return 0 if the attributes for two types are incompatible, 1 if they
2120    are compatible, and 2 if they are nearly compatible (which causes a
2121    warning to be generated).  */
2122
2123 static int
2124 ix86_comp_type_attributes (tree type1, tree type2)
2125 {
2126   /* Check for mismatch of non-default calling convention.  */
2127   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2128
2129   if (TREE_CODE (type1) != FUNCTION_TYPE)
2130     return 1;
2131
2132   /* Check for mismatched fastcall/regparm types.  */
2133   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2134        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2135       || (ix86_function_regparm (type1, NULL)
2136           != ix86_function_regparm (type2, NULL)))
2137     return 0;
2138
2139   /* Check for mismatched sseregparm types.  */
2140   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2141       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2142     return 0;
2143
2144   /* Check for mismatched return types (cdecl vs stdcall).  */
2145   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2146       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2147     return 0;
2148
2149   return 1;
2150 }
2151 \f
2152 /* Return the regparm value for a function with the indicated TYPE and DECL.
2153    DECL may be NULL when calling function indirectly
2154    or considering a libcall.  */
2155
2156 static int
2157 ix86_function_regparm (tree type, tree decl)
2158 {
2159   tree attr;
2160   int regparm = ix86_regparm;
2161   bool user_convention = false;
2162
2163   if (!TARGET_64BIT)
2164     {
2165       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2166       if (attr)
2167         {
2168           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2169           user_convention = true;
2170         }
2171
2172       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2173         {
2174           regparm = 2;
2175           user_convention = true;
2176         }
2177
2178       /* Use register calling convention for local functions when possible.  */
2179       if (!TARGET_64BIT && !user_convention && decl
2180           && flag_unit_at_a_time && !profile_flag)
2181         {
2182           struct cgraph_local_info *i = cgraph_local_info (decl);
2183           if (i && i->local)
2184             {
2185               int local_regparm, globals = 0, regno;
2186
2187               /* Make sure no regparm register is taken by a global register
2188                  variable.  */
2189               for (local_regparm = 0; local_regparm < 3; local_regparm++)
2190                 if (global_regs[local_regparm])
2191                   break;
2192               /* We can't use regparm(3) for nested functions as these use
2193                  static chain pointer in third argument.  */
2194               if (local_regparm == 3
2195                   && decl_function_context (decl)
2196                   && !DECL_NO_STATIC_CHAIN (decl))
2197                 local_regparm = 2;
2198               /* Each global register variable increases register preassure,
2199                  so the more global reg vars there are, the smaller regparm
2200                  optimization use, unless requested by the user explicitly.  */
2201               for (regno = 0; regno < 6; regno++)
2202                 if (global_regs[regno])
2203                   globals++;
2204               local_regparm
2205                 = globals < local_regparm ? local_regparm - globals : 0;
2206
2207               if (local_regparm > regparm)
2208                 regparm = local_regparm;
2209             }
2210         }
2211     }
2212   return regparm;
2213 }
2214
2215 /* Return 1 or 2, if we can pass up to 8 SFmode (1) and DFmode (2) arguments
2216    in SSE registers for a function with the indicated TYPE and DECL.
2217    DECL may be NULL when calling function indirectly
2218    or considering a libcall.  Otherwise return 0.  */
2219
2220 static int
2221 ix86_function_sseregparm (tree type, tree decl)
2222 {
2223   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2224      by the sseregparm attribute.  */
2225   if (TARGET_SSEREGPARM
2226       || (type
2227           && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
2228     {
2229       if (!TARGET_SSE)
2230         {
2231           if (decl)
2232             error ("Calling %qD with attribute sseregparm without "
2233                    "SSE/SSE2 enabled", decl);
2234           else
2235             error ("Calling %qT with attribute sseregparm without "
2236                    "SSE/SSE2 enabled", type);
2237           return 0;
2238         }
2239
2240       return 2;
2241     }
2242
2243   /* For local functions, pass SFmode (and DFmode for SSE2) arguments
2244      in SSE registers even for 32-bit mode and not just 3, but up to
2245      8 SSE arguments in registers.  */
2246   if (!TARGET_64BIT && decl
2247       && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2248     {
2249       struct cgraph_local_info *i = cgraph_local_info (decl);
2250       if (i && i->local)
2251         return TARGET_SSE2 ? 2 : 1;
2252     }
2253
2254   return 0;
2255 }
2256
2257 /* Return true if EAX is live at the start of the function.  Used by
2258    ix86_expand_prologue to determine if we need special help before
2259    calling allocate_stack_worker.  */
2260
2261 static bool
2262 ix86_eax_live_at_start_p (void)
2263 {
2264   /* Cheat.  Don't bother working forward from ix86_function_regparm
2265      to the function type to whether an actual argument is located in
2266      eax.  Instead just look at cfg info, which is still close enough
2267      to correct at this point.  This gives false positives for broken
2268      functions that might use uninitialized data that happens to be
2269      allocated in eax, but who cares?  */
2270   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2271 }
2272
2273 /* Value is the number of bytes of arguments automatically
2274    popped when returning from a subroutine call.
2275    FUNDECL is the declaration node of the function (as a tree),
2276    FUNTYPE is the data type of the function (as a tree),
2277    or for a library call it is an identifier node for the subroutine name.
2278    SIZE is the number of bytes of arguments passed on the stack.
2279
2280    On the 80386, the RTD insn may be used to pop them if the number
2281      of args is fixed, but if the number is variable then the caller
2282      must pop them all.  RTD can't be used for library calls now
2283      because the library is compiled with the Unix compiler.
2284    Use of RTD is a selectable option, since it is incompatible with
2285    standard Unix calling sequences.  If the option is not selected,
2286    the caller must always pop the args.
2287
2288    The attribute stdcall is equivalent to RTD on a per module basis.  */
2289
2290 int
2291 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2292 {
2293   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2294
2295   /* Cdecl functions override -mrtd, and never pop the stack.  */
2296   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
2297
2298     /* Stdcall and fastcall functions will pop the stack if not
2299        variable args.  */
2300     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2301         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2302       rtd = 1;
2303
2304     if (rtd
2305         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
2306             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
2307                 == void_type_node)))
2308       return size;
2309   }
2310
2311   /* Lose any fake structure return argument if it is passed on the stack.  */
2312   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2313       && !TARGET_64BIT
2314       && !KEEP_AGGREGATE_RETURN_POINTER)
2315     {
2316       int nregs = ix86_function_regparm (funtype, fundecl);
2317
2318       if (!nregs)
2319         return GET_MODE_SIZE (Pmode);
2320     }
2321
2322   return 0;
2323 }
2324 \f
2325 /* Argument support functions.  */
2326
2327 /* Return true when register may be used to pass function parameters.  */
2328 bool
2329 ix86_function_arg_regno_p (int regno)
2330 {
2331   int i;
2332   if (!TARGET_64BIT)
2333     return (regno < REGPARM_MAX
2334             || (TARGET_MMX && MMX_REGNO_P (regno)
2335                 && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2336             || (TARGET_SSE && SSE_REGNO_P (regno)
2337                 && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2338
2339   if (TARGET_SSE && SSE_REGNO_P (regno)
2340       && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2341     return true;
2342   /* RAX is used as hidden argument to va_arg functions.  */
2343   if (!regno)
2344     return true;
2345   for (i = 0; i < REGPARM_MAX; i++)
2346     if (regno == x86_64_int_parameter_registers[i])
2347       return true;
2348   return false;
2349 }
2350
2351 /* Return if we do not know how to pass TYPE solely in registers.  */
2352
2353 static bool
2354 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2355 {
2356   if (must_pass_in_stack_var_size_or_pad (mode, type))
2357     return true;
2358
2359   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2360      The layout_type routine is crafty and tries to trick us into passing
2361      currently unsupported vector types on the stack by using TImode.  */
2362   return (!TARGET_64BIT && mode == TImode
2363           && type && TREE_CODE (type) != VECTOR_TYPE);
2364 }
2365
2366 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2367    for a call to a function whose data type is FNTYPE.
2368    For a library call, FNTYPE is 0.  */
2369
2370 void
2371 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
2372                       tree fntype,      /* tree ptr for function decl */
2373                       rtx libname,      /* SYMBOL_REF of library name or 0 */
2374                       tree fndecl)
2375 {
2376   static CUMULATIVE_ARGS zero_cum;
2377   tree param, next_param;
2378
2379   if (TARGET_DEBUG_ARG)
2380     {
2381       fprintf (stderr, "\ninit_cumulative_args (");
2382       if (fntype)
2383         fprintf (stderr, "fntype code = %s, ret code = %s",
2384                  tree_code_name[(int) TREE_CODE (fntype)],
2385                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
2386       else
2387         fprintf (stderr, "no fntype");
2388
2389       if (libname)
2390         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
2391     }
2392
2393   *cum = zero_cum;
2394
2395   /* Set up the number of registers to use for passing arguments.  */
2396   cum->nregs = ix86_regparm;
2397   if (TARGET_SSE)
2398     cum->sse_nregs = SSE_REGPARM_MAX;
2399   if (TARGET_MMX)
2400     cum->mmx_nregs = MMX_REGPARM_MAX;
2401   cum->warn_sse = true;
2402   cum->warn_mmx = true;
2403   cum->maybe_vaarg = false;
2404
2405   /* Use ecx and edx registers if function has fastcall attribute,
2406      else look for regparm information.  */
2407   if (fntype && !TARGET_64BIT)
2408     {
2409       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
2410         {
2411           cum->nregs = 2;
2412           cum->fastcall = 1;
2413         }
2414       else
2415         cum->nregs = ix86_function_regparm (fntype, fndecl);
2416     }
2417
2418   /* Set up the number of SSE registers used for passing SFmode
2419      and DFmode arguments.  Warn for mismatching ABI.  */
2420   cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
2421
2422   /* Determine if this function has variable arguments.  This is
2423      indicated by the last argument being 'void_type_mode' if there
2424      are no variable arguments.  If there are variable arguments, then
2425      we won't pass anything in registers in 32-bit mode. */
2426
2427   if (cum->nregs || cum->mmx_nregs || cum->sse_nregs)
2428     {
2429       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
2430            param != 0; param = next_param)
2431         {
2432           next_param = TREE_CHAIN (param);
2433           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
2434             {
2435               if (!TARGET_64BIT)
2436                 {
2437                   cum->nregs = 0;
2438                   cum->sse_nregs = 0;
2439                   cum->mmx_nregs = 0;
2440                   cum->warn_sse = 0;
2441                   cum->warn_mmx = 0;
2442                   cum->fastcall = 0;
2443                   cum->float_in_sse = 0;
2444                 }
2445               cum->maybe_vaarg = true;
2446             }
2447         }
2448     }
2449   if ((!fntype && !libname)
2450       || (fntype && !TYPE_ARG_TYPES (fntype)))
2451     cum->maybe_vaarg = true;
2452
2453   if (TARGET_DEBUG_ARG)
2454     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
2455
2456   return;
2457 }
2458
2459 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
2460    But in the case of vector types, it is some vector mode.
2461
2462    When we have only some of our vector isa extensions enabled, then there
2463    are some modes for which vector_mode_supported_p is false.  For these
2464    modes, the generic vector support in gcc will choose some non-vector mode
2465    in order to implement the type.  By computing the natural mode, we'll 
2466    select the proper ABI location for the operand and not depend on whatever
2467    the middle-end decides to do with these vector types.  */
2468
2469 static enum machine_mode
2470 type_natural_mode (tree type)
2471 {
2472   enum machine_mode mode = TYPE_MODE (type);
2473
2474   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
2475     {
2476       HOST_WIDE_INT size = int_size_in_bytes (type);
2477       if ((size == 8 || size == 16)
2478           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
2479           && TYPE_VECTOR_SUBPARTS (type) > 1)
2480         {
2481           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
2482
2483           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
2484             mode = MIN_MODE_VECTOR_FLOAT;
2485           else
2486             mode = MIN_MODE_VECTOR_INT;
2487
2488           /* Get the mode which has this inner mode and number of units.  */
2489           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2490             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
2491                 && GET_MODE_INNER (mode) == innermode)
2492               return mode;
2493
2494           gcc_unreachable ();
2495         }
2496     }
2497
2498   return mode;
2499 }
2500
2501 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
2502    this may not agree with the mode that the type system has chosen for the
2503    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
2504    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
2505
2506 static rtx
2507 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
2508                      unsigned int regno)
2509 {
2510   rtx tmp;
2511
2512   if (orig_mode != BLKmode)
2513     tmp = gen_rtx_REG (orig_mode, regno);
2514   else
2515     {
2516       tmp = gen_rtx_REG (mode, regno);
2517       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
2518       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
2519     }
2520
2521   return tmp;
2522 }
2523
2524 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
2525    of this code is to classify each 8bytes of incoming argument by the register
2526    class and assign registers accordingly.  */
2527
2528 /* Return the union class of CLASS1 and CLASS2.
2529    See the x86-64 PS ABI for details.  */
2530
2531 static enum x86_64_reg_class
2532 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
2533 {
2534   /* Rule #1: If both classes are equal, this is the resulting class.  */
2535   if (class1 == class2)
2536     return class1;
2537
2538   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
2539      the other class.  */
2540   if (class1 == X86_64_NO_CLASS)
2541     return class2;
2542   if (class2 == X86_64_NO_CLASS)
2543     return class1;
2544
2545   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
2546   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
2547     return X86_64_MEMORY_CLASS;
2548
2549   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
2550   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
2551       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
2552     return X86_64_INTEGERSI_CLASS;
2553   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
2554       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
2555     return X86_64_INTEGER_CLASS;
2556
2557   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
2558      MEMORY is used.  */
2559   if (class1 == X86_64_X87_CLASS
2560       || class1 == X86_64_X87UP_CLASS
2561       || class1 == X86_64_COMPLEX_X87_CLASS
2562       || class2 == X86_64_X87_CLASS
2563       || class2 == X86_64_X87UP_CLASS
2564       || class2 == X86_64_COMPLEX_X87_CLASS)
2565     return X86_64_MEMORY_CLASS;
2566
2567   /* Rule #6: Otherwise class SSE is used.  */
2568   return X86_64_SSE_CLASS;
2569 }
2570
2571 /* Classify the argument of type TYPE and mode MODE.
2572    CLASSES will be filled by the register class used to pass each word
2573    of the operand.  The number of words is returned.  In case the parameter
2574    should be passed in memory, 0 is returned. As a special case for zero
2575    sized containers, classes[0] will be NO_CLASS and 1 is returned.
2576
2577    BIT_OFFSET is used internally for handling records and specifies offset
2578    of the offset in bits modulo 256 to avoid overflow cases.
2579
2580    See the x86-64 PS ABI for details.
2581 */
2582
2583 static int
2584 classify_argument (enum machine_mode mode, tree type,
2585                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
2586 {
2587   HOST_WIDE_INT bytes =
2588     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2589   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2590
2591   /* Variable sized entities are always passed/returned in memory.  */
2592   if (bytes < 0)
2593     return 0;
2594
2595   if (mode != VOIDmode
2596       && targetm.calls.must_pass_in_stack (mode, type))
2597     return 0;
2598
2599   if (type && AGGREGATE_TYPE_P (type))
2600     {
2601       int i;
2602       tree field;
2603       enum x86_64_reg_class subclasses[MAX_CLASSES];
2604
2605       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
2606       if (bytes > 16)
2607         return 0;
2608
2609       for (i = 0; i < words; i++)
2610         classes[i] = X86_64_NO_CLASS;
2611
2612       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
2613          signalize memory class, so handle it as special case.  */
2614       if (!words)
2615         {
2616           classes[0] = X86_64_NO_CLASS;
2617           return 1;
2618         }
2619
2620       /* Classify each field of record and merge classes.  */
2621       switch (TREE_CODE (type))
2622         {
2623         case RECORD_TYPE:
2624           /* For classes first merge in the field of the subclasses.  */
2625           if (TYPE_BINFO (type))
2626             {
2627               tree binfo, base_binfo;
2628               int basenum;
2629
2630               for (binfo = TYPE_BINFO (type), basenum = 0;
2631                    BINFO_BASE_ITERATE (binfo, basenum, base_binfo); basenum++)
2632                 {
2633                    int num;
2634                    int offset = tree_low_cst (BINFO_OFFSET (base_binfo), 0) * 8;
2635                    tree type = BINFO_TYPE (base_binfo);
2636
2637                    num = classify_argument (TYPE_MODE (type),
2638                                             type, subclasses,
2639                                             (offset + bit_offset) % 256);
2640                    if (!num)
2641                      return 0;
2642                    for (i = 0; i < num; i++)
2643                      {
2644                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2645                        classes[i + pos] =
2646                          merge_classes (subclasses[i], classes[i + pos]);
2647                      }
2648                 }
2649             }
2650           /* And now merge the fields of structure.  */
2651           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2652             {
2653               if (TREE_CODE (field) == FIELD_DECL)
2654                 {
2655                   int num;
2656
2657                   /* Bitfields are always classified as integer.  Handle them
2658                      early, since later code would consider them to be
2659                      misaligned integers.  */
2660                   if (DECL_BIT_FIELD (field))
2661                     {
2662                       for (i = int_bit_position (field) / 8 / 8;
2663                            i < (int_bit_position (field)
2664                                 + tree_low_cst (DECL_SIZE (field), 0)
2665                                 + 63) / 8 / 8; i++)
2666                         classes[i] =
2667                           merge_classes (X86_64_INTEGER_CLASS,
2668                                          classes[i]);
2669                     }
2670                   else
2671                     {
2672                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2673                                                TREE_TYPE (field), subclasses,
2674                                                (int_bit_position (field)
2675                                                 + bit_offset) % 256);
2676                       if (!num)
2677                         return 0;
2678                       for (i = 0; i < num; i++)
2679                         {
2680                           int pos =
2681                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
2682                           classes[i + pos] =
2683                             merge_classes (subclasses[i], classes[i + pos]);
2684                         }
2685                     }
2686                 }
2687             }
2688           break;
2689
2690         case ARRAY_TYPE:
2691           /* Arrays are handled as small records.  */
2692           {
2693             int num;
2694             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
2695                                      TREE_TYPE (type), subclasses, bit_offset);
2696             if (!num)
2697               return 0;
2698
2699             /* The partial classes are now full classes.  */
2700             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
2701               subclasses[0] = X86_64_SSE_CLASS;
2702             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
2703               subclasses[0] = X86_64_INTEGER_CLASS;
2704             
2705             for (i = 0; i < words; i++)
2706               classes[i] = subclasses[i % num];
2707             
2708             break;
2709           }
2710         case UNION_TYPE:
2711         case QUAL_UNION_TYPE:
2712           /* Unions are similar to RECORD_TYPE but offset is always 0.
2713              */
2714
2715           /* Unions are not derived.  */
2716           gcc_assert (!TYPE_BINFO (type)
2717                       || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
2718           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2719             {
2720               if (TREE_CODE (field) == FIELD_DECL)
2721                 {
2722                   int num;
2723                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2724                                            TREE_TYPE (field), subclasses,
2725                                            bit_offset);
2726                   if (!num)
2727                     return 0;
2728                   for (i = 0; i < num; i++)
2729                     classes[i] = merge_classes (subclasses[i], classes[i]);
2730                 }
2731             }
2732           break;
2733
2734         default:
2735           gcc_unreachable ();
2736         }
2737
2738       /* Final merger cleanup.  */
2739       for (i = 0; i < words; i++)
2740         {
2741           /* If one class is MEMORY, everything should be passed in
2742              memory.  */
2743           if (classes[i] == X86_64_MEMORY_CLASS)
2744             return 0;
2745
2746           /* The X86_64_SSEUP_CLASS should be always preceded by
2747              X86_64_SSE_CLASS.  */
2748           if (classes[i] == X86_64_SSEUP_CLASS
2749               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
2750             classes[i] = X86_64_SSE_CLASS;
2751
2752           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
2753           if (classes[i] == X86_64_X87UP_CLASS
2754               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
2755             classes[i] = X86_64_SSE_CLASS;
2756         }
2757       return words;
2758     }
2759
2760   /* Compute alignment needed.  We align all types to natural boundaries with
2761      exception of XFmode that is aligned to 64bits.  */
2762   if (mode != VOIDmode && mode != BLKmode)
2763     {
2764       int mode_alignment = GET_MODE_BITSIZE (mode);
2765
2766       if (mode == XFmode)
2767         mode_alignment = 128;
2768       else if (mode == XCmode)
2769         mode_alignment = 256;
2770       if (COMPLEX_MODE_P (mode))
2771         mode_alignment /= 2;
2772       /* Misaligned fields are always returned in memory.  */
2773       if (bit_offset % mode_alignment)
2774         return 0;
2775     }
2776
2777   /* for V1xx modes, just use the base mode */
2778   if (VECTOR_MODE_P (mode)
2779       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
2780     mode = GET_MODE_INNER (mode);
2781
2782   /* Classification of atomic types.  */
2783   switch (mode)
2784     {
2785     case DImode:
2786     case SImode:
2787     case HImode:
2788     case QImode:
2789     case CSImode:
2790     case CHImode:
2791     case CQImode:
2792       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2793         classes[0] = X86_64_INTEGERSI_CLASS;
2794       else
2795         classes[0] = X86_64_INTEGER_CLASS;
2796       return 1;
2797     case CDImode:
2798     case TImode:
2799       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
2800       return 2;
2801     case CTImode:
2802       return 0;
2803     case SFmode:
2804       if (!(bit_offset % 64))
2805         classes[0] = X86_64_SSESF_CLASS;
2806       else
2807         classes[0] = X86_64_SSE_CLASS;
2808       return 1;
2809     case DFmode:
2810       classes[0] = X86_64_SSEDF_CLASS;
2811       return 1;
2812     case XFmode:
2813       classes[0] = X86_64_X87_CLASS;
2814       classes[1] = X86_64_X87UP_CLASS;
2815       return 2;
2816     case TFmode:
2817       classes[0] = X86_64_SSE_CLASS;
2818       classes[1] = X86_64_SSEUP_CLASS;
2819       return 2;
2820     case SCmode:
2821       classes[0] = X86_64_SSE_CLASS;
2822       return 1;
2823     case DCmode:
2824       classes[0] = X86_64_SSEDF_CLASS;
2825       classes[1] = X86_64_SSEDF_CLASS;
2826       return 2;
2827     case XCmode:
2828       classes[0] = X86_64_COMPLEX_X87_CLASS;
2829       return 1;
2830     case TCmode:
2831       /* This modes is larger than 16 bytes.  */
2832       return 0;
2833     case V4SFmode:
2834     case V4SImode:
2835     case V16QImode:
2836     case V8HImode:
2837     case V2DFmode:
2838     case V2DImode:
2839       classes[0] = X86_64_SSE_CLASS;
2840       classes[1] = X86_64_SSEUP_CLASS;
2841       return 2;
2842     case V2SFmode:
2843     case V2SImode:
2844     case V4HImode:
2845     case V8QImode:
2846       classes[0] = X86_64_SSE_CLASS;
2847       return 1;
2848     case BLKmode:
2849     case VOIDmode:
2850       return 0;
2851     default:
2852       gcc_assert (VECTOR_MODE_P (mode));
2853       
2854       if (bytes > 16)
2855         return 0;
2856       
2857       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
2858       
2859       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2860         classes[0] = X86_64_INTEGERSI_CLASS;
2861       else
2862         classes[0] = X86_64_INTEGER_CLASS;
2863       classes[1] = X86_64_INTEGER_CLASS;
2864       return 1 + (bytes > 8);
2865     }
2866 }
2867
2868 /* Examine the argument and return set number of register required in each
2869    class.  Return 0 iff parameter should be passed in memory.  */
2870 static int
2871 examine_argument (enum machine_mode mode, tree type, int in_return,
2872                   int *int_nregs, int *sse_nregs)
2873 {
2874   enum x86_64_reg_class class[MAX_CLASSES];
2875   int n = classify_argument (mode, type, class, 0);
2876
2877   *int_nregs = 0;
2878   *sse_nregs = 0;
2879   if (!n)
2880     return 0;
2881   for (n--; n >= 0; n--)
2882     switch (class[n])
2883       {
2884       case X86_64_INTEGER_CLASS:
2885       case X86_64_INTEGERSI_CLASS:
2886         (*int_nregs)++;
2887         break;
2888       case X86_64_SSE_CLASS:
2889       case X86_64_SSESF_CLASS:
2890       case X86_64_SSEDF_CLASS:
2891         (*sse_nregs)++;
2892         break;
2893       case X86_64_NO_CLASS:
2894       case X86_64_SSEUP_CLASS:
2895         break;
2896       case X86_64_X87_CLASS:
2897       case X86_64_X87UP_CLASS:
2898         if (!in_return)
2899           return 0;
2900         break;
2901       case X86_64_COMPLEX_X87_CLASS:
2902         return in_return ? 2 : 0;
2903       case X86_64_MEMORY_CLASS:
2904         gcc_unreachable ();
2905       }
2906   return 1;
2907 }
2908
2909 /* Construct container for the argument used by GCC interface.  See
2910    FUNCTION_ARG for the detailed description.  */
2911
2912 static rtx
2913 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
2914                      tree type, int in_return, int nintregs, int nsseregs,
2915                      const int *intreg, int sse_regno)
2916 {
2917   enum machine_mode tmpmode;
2918   int bytes =
2919     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2920   enum x86_64_reg_class class[MAX_CLASSES];
2921   int n;
2922   int i;
2923   int nexps = 0;
2924   int needed_sseregs, needed_intregs;
2925   rtx exp[MAX_CLASSES];
2926   rtx ret;
2927
2928   n = classify_argument (mode, type, class, 0);
2929   if (TARGET_DEBUG_ARG)
2930     {
2931       if (!n)
2932         fprintf (stderr, "Memory class\n");
2933       else
2934         {
2935           fprintf (stderr, "Classes:");
2936           for (i = 0; i < n; i++)
2937             {
2938               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
2939             }
2940            fprintf (stderr, "\n");
2941         }
2942     }
2943   if (!n)
2944     return NULL;
2945   if (!examine_argument (mode, type, in_return, &needed_intregs,
2946                          &needed_sseregs))
2947     return NULL;
2948   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
2949     return NULL;
2950
2951   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
2952      some less clueful developer tries to use floating-point anyway.  */
2953   if (needed_sseregs && !TARGET_SSE)
2954     {
2955       static bool issued_error;
2956       if (!issued_error)
2957         {
2958           issued_error = true;
2959           if (in_return)
2960             error ("SSE register return with SSE disabled");
2961           else
2962             error ("SSE register argument with SSE disabled");
2963         }
2964       return NULL;
2965     }
2966
2967   /* First construct simple cases.  Avoid SCmode, since we want to use
2968      single register to pass this type.  */
2969   if (n == 1 && mode != SCmode)
2970     switch (class[0])
2971       {
2972       case X86_64_INTEGER_CLASS:
2973       case X86_64_INTEGERSI_CLASS:
2974         return gen_rtx_REG (mode, intreg[0]);
2975       case X86_64_SSE_CLASS:
2976       case X86_64_SSESF_CLASS:
2977       case X86_64_SSEDF_CLASS:
2978         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
2979       case X86_64_X87_CLASS:
2980       case X86_64_COMPLEX_X87_CLASS:
2981         return gen_rtx_REG (mode, FIRST_STACK_REG);
2982       case X86_64_NO_CLASS:
2983         /* Zero sized array, struct or class.  */
2984         return NULL;
2985       default:
2986         gcc_unreachable ();
2987       }
2988   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
2989       && mode != BLKmode)
2990     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2991   if (n == 2
2992       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
2993     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
2994   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
2995       && class[1] == X86_64_INTEGER_CLASS
2996       && (mode == CDImode || mode == TImode || mode == TFmode)
2997       && intreg[0] + 1 == intreg[1])
2998     return gen_rtx_REG (mode, intreg[0]);
2999
3000   /* Otherwise figure out the entries of the PARALLEL.  */
3001   for (i = 0; i < n; i++)
3002     {
3003       switch (class[i])
3004         {
3005           case X86_64_NO_CLASS:
3006             break;
3007           case X86_64_INTEGER_CLASS:
3008           case X86_64_INTEGERSI_CLASS:
3009             /* Merge TImodes on aligned occasions here too.  */
3010             if (i * 8 + 8 > bytes)
3011               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3012             else if (class[i] == X86_64_INTEGERSI_CLASS)
3013               tmpmode = SImode;
3014             else
3015               tmpmode = DImode;
3016             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3017             if (tmpmode == BLKmode)
3018               tmpmode = DImode;
3019             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3020                                                gen_rtx_REG (tmpmode, *intreg),
3021                                                GEN_INT (i*8));
3022             intreg++;
3023             break;
3024           case X86_64_SSESF_CLASS:
3025             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3026                                                gen_rtx_REG (SFmode,
3027                                                             SSE_REGNO (sse_regno)),
3028                                                GEN_INT (i*8));
3029             sse_regno++;
3030             break;
3031           case X86_64_SSEDF_CLASS:
3032             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3033                                                gen_rtx_REG (DFmode,
3034                                                             SSE_REGNO (sse_regno)),
3035                                                GEN_INT (i*8));
3036             sse_regno++;
3037             break;
3038           case X86_64_SSE_CLASS:
3039             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3040               tmpmode = TImode;
3041             else
3042               tmpmode = DImode;
3043             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3044                                                gen_rtx_REG (tmpmode,
3045                                                             SSE_REGNO (sse_regno)),
3046                                                GEN_INT (i*8));
3047             if (tmpmode == TImode)
3048               i++;
3049             sse_regno++;
3050             break;
3051           default:
3052             gcc_unreachable ();
3053         }
3054     }
3055
3056   /* Empty aligned struct, union or class.  */
3057   if (nexps == 0)
3058     return NULL;
3059
3060   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3061   for (i = 0; i < nexps; i++)
3062     XVECEXP (ret, 0, i) = exp [i];
3063   return ret;
3064 }
3065
3066 /* Update the data in CUM to advance over an argument
3067    of mode MODE and data type TYPE.
3068    (TYPE is null for libcalls where that information may not be available.)  */
3069
3070 void
3071 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3072                       tree type, int named)
3073 {
3074   int bytes =
3075     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3076   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3077
3078   if (type)
3079     mode = type_natural_mode (type);
3080
3081   if (TARGET_DEBUG_ARG)
3082     fprintf (stderr, "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, "
3083              "mode=%s, named=%d)\n\n",
3084              words, cum->words, cum->nregs, cum->sse_nregs,
3085              GET_MODE_NAME (mode), named);
3086
3087   if (TARGET_64BIT)
3088     {
3089       int int_nregs, sse_nregs;
3090       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3091         cum->words += words;
3092       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3093         {
3094           cum->nregs -= int_nregs;
3095           cum->sse_nregs -= sse_nregs;
3096           cum->regno += int_nregs;
3097           cum->sse_regno += sse_nregs;
3098         }
3099       else
3100         cum->words += words;
3101     }
3102   else
3103     {
3104       switch (mode)
3105         {
3106         default:
3107           break;
3108
3109         case BLKmode:
3110           if (bytes < 0)
3111             break;
3112           /* FALLTHRU */
3113
3114         case DImode:
3115         case SImode:
3116         case HImode:
3117         case QImode:
3118           cum->words += words;
3119           cum->nregs -= words;
3120           cum->regno += words;
3121
3122           if (cum->nregs <= 0)
3123             {
3124               cum->nregs = 0;
3125               cum->regno = 0;
3126             }
3127           break;
3128
3129         case DFmode:
3130           if (cum->float_in_sse < 2)
3131             break;
3132         case SFmode:
3133           if (cum->float_in_sse < 1)
3134             break;
3135           /* FALLTHRU */
3136
3137         case TImode:
3138         case V16QImode:
3139         case V8HImode:
3140         case V4SImode:
3141         case V2DImode:
3142         case V4SFmode:
3143         case V2DFmode:
3144           if (!type || !AGGREGATE_TYPE_P (type))
3145             {
3146               cum->sse_words += words;
3147               cum->sse_nregs -= 1;
3148               cum->sse_regno += 1;
3149               if (cum->sse_nregs <= 0)
3150                 {
3151                   cum->sse_nregs = 0;
3152                   cum->sse_regno = 0;
3153                 }
3154             }
3155           break;
3156
3157         case V8QImode:
3158         case V4HImode:
3159         case V2SImode:
3160         case V2SFmode:
3161           if (!type || !AGGREGATE_TYPE_P (type))
3162             {
3163               cum->mmx_words += words;
3164               cum->mmx_nregs -= 1;
3165               cum->mmx_regno += 1;
3166               if (cum->mmx_nregs <= 0)
3167                 {
3168                   cum->mmx_nregs = 0;
3169                   cum->mmx_regno = 0;
3170                 }
3171             }
3172           break;
3173         }
3174     }
3175 }
3176
3177 /* Define where to put the arguments to a function.
3178    Value is zero to push the argument on the stack,
3179    or a hard register in which to store the argument.
3180
3181    MODE is the argument's machine mode.
3182    TYPE is the data type of the argument (as a tree).
3183     This is null for libcalls where that information may
3184     not be available.
3185    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3186     the preceding args and about the function being called.
3187    NAMED is nonzero if this argument is a named parameter
3188     (otherwise it is an extra parameter matching an ellipsis).  */
3189
3190 rtx
3191 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode orig_mode,
3192               tree type, int named)
3193 {
3194   enum machine_mode mode = orig_mode;
3195   rtx ret = NULL_RTX;
3196   int bytes =
3197     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3198   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3199   static bool warnedsse, warnedmmx;
3200
3201   /* To simplify the code below, represent vector types with a vector mode
3202      even if MMX/SSE are not active.  */
3203   if (type && TREE_CODE (type) == VECTOR_TYPE)
3204     mode = type_natural_mode (type);
3205
3206   /* Handle a hidden AL argument containing number of registers for varargs
3207      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
3208      any AL settings.  */
3209   if (mode == VOIDmode)
3210     {
3211       if (TARGET_64BIT)
3212         return GEN_INT (cum->maybe_vaarg
3213                         ? (cum->sse_nregs < 0
3214                            ? SSE_REGPARM_MAX
3215                            : cum->sse_regno)
3216                         : -1);
3217       else
3218         return constm1_rtx;
3219     }
3220   if (TARGET_64BIT)
3221     ret = construct_container (mode, orig_mode, type, 0, cum->nregs,
3222                                cum->sse_nregs,
3223                                &x86_64_int_parameter_registers [cum->regno],
3224                                cum->sse_regno);
3225   else
3226     switch (mode)
3227       {
3228         /* For now, pass fp/complex values on the stack.  */
3229       default:
3230         break;
3231
3232       case BLKmode:
3233         if (bytes < 0)
3234           break;
3235         /* FALLTHRU */
3236       case DImode:
3237       case SImode:
3238       case HImode:
3239       case QImode:
3240         if (words <= cum->nregs)
3241           {
3242             int regno = cum->regno;
3243
3244             /* Fastcall allocates the first two DWORD (SImode) or
3245                smaller arguments to ECX and EDX.  */
3246             if (cum->fastcall)
3247               {
3248                 if (mode == BLKmode || mode == DImode)
3249                   break;
3250
3251                 /* ECX not EAX is the first allocated register.  */
3252                 if (regno == 0)
3253                   regno = 2;
3254               }
3255             ret = gen_rtx_REG (mode, regno);
3256           }
3257         break;
3258       case DFmode:
3259         if (cum->float_in_sse < 2)
3260           break;
3261       case SFmode:
3262         if (cum->float_in_sse < 1)
3263           break;
3264         /* FALLTHRU */
3265       case TImode:
3266       case V16QImode:
3267       case V8HImode:
3268       case V4SImode:
3269       case V2DImode:
3270       case V4SFmode:
3271       case V2DFmode:
3272         if (!type || !AGGREGATE_TYPE_P (type))
3273           {
3274             if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3275               {
3276                 warnedsse = true;
3277                 warning (0, "SSE vector argument without SSE enabled "
3278                          "changes the ABI");
3279               }
3280             if (cum->sse_nregs)
3281               ret = gen_reg_or_parallel (mode, orig_mode,
3282                                          cum->sse_regno + FIRST_SSE_REG);
3283           }
3284         break;
3285       case V8QImode:
3286       case V4HImode:
3287       case V2SImode:
3288       case V2SFmode:
3289         if (!type || !AGGREGATE_TYPE_P (type))
3290           {
3291             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3292               {
3293                 warnedmmx = true;
3294                 warning (0, "MMX vector argument without MMX enabled "
3295                          "changes the ABI");
3296               }
3297             if (cum->mmx_nregs)
3298               ret = gen_reg_or_parallel (mode, orig_mode,
3299                                          cum->mmx_regno + FIRST_MMX_REG);
3300           }
3301         break;
3302       }
3303
3304   if (TARGET_DEBUG_ARG)
3305     {
3306       fprintf (stderr,
3307                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
3308                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
3309
3310       if (ret)
3311         print_simple_rtl (stderr, ret);
3312       else
3313         fprintf (stderr, ", stack");
3314
3315       fprintf (stderr, " )\n");
3316     }
3317
3318   return ret;
3319 }
3320
3321 /* A C expression that indicates when an argument must be passed by
3322    reference.  If nonzero for an argument, a copy of that argument is
3323    made in memory and a pointer to the argument is passed instead of
3324    the argument itself.  The pointer is passed in whatever way is
3325    appropriate for passing a pointer to that type.  */
3326
3327 static bool
3328 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3329                         enum machine_mode mode ATTRIBUTE_UNUSED,
3330                         tree type, bool named ATTRIBUTE_UNUSED)
3331 {
3332   if (!TARGET_64BIT)
3333     return 0;
3334
3335   if (type && int_size_in_bytes (type) == -1)
3336     {
3337       if (TARGET_DEBUG_ARG)
3338         fprintf (stderr, "function_arg_pass_by_reference\n");
3339       return 1;
3340     }
3341
3342   return 0;
3343 }
3344
3345 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
3346    ABI.  Only called if TARGET_SSE.  */
3347 static bool
3348 contains_128bit_aligned_vector_p (tree type)
3349 {
3350   enum machine_mode mode = TYPE_MODE (type);
3351   if (SSE_REG_MODE_P (mode)
3352       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
3353     return true;
3354   if (TYPE_ALIGN (type) < 128)
3355     return false;
3356
3357   if (AGGREGATE_TYPE_P (type))
3358     {
3359       /* Walk the aggregates recursively.  */
3360       switch (TREE_CODE (type))
3361         {
3362         case RECORD_TYPE:
3363         case UNION_TYPE:
3364         case QUAL_UNION_TYPE:
3365           {
3366             tree field;
3367             
3368             if (TYPE_BINFO (type))
3369               {
3370                 tree binfo, base_binfo;
3371                 int i;
3372                 
3373                 for (binfo = TYPE_BINFO (type), i = 0;
3374                      BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3375                   if (contains_128bit_aligned_vector_p
3376                       (BINFO_TYPE (base_binfo)))
3377                     return true;
3378               }
3379             /* And now merge the fields of structure.  */
3380             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3381               {
3382                 if (TREE_CODE (field) == FIELD_DECL
3383                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
3384                   return true;
3385               }
3386             break;
3387           }
3388
3389         case ARRAY_TYPE:
3390           /* Just for use if some languages passes arrays by value.  */
3391           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
3392             return true;
3393           break;
3394           
3395         default:
3396           gcc_unreachable ();
3397         }
3398     }
3399   return false;
3400 }
3401
3402 /* Gives the alignment boundary, in bits, of an argument with the
3403    specified mode and type.  */
3404
3405 int
3406 ix86_function_arg_boundary (enum machine_mode mode, tree type)
3407 {
3408   int align;
3409   if (type)
3410     align = TYPE_ALIGN (type);
3411   else
3412     align = GET_MODE_ALIGNMENT (mode);
3413   if (align < PARM_BOUNDARY)
3414     align = PARM_BOUNDARY;
3415   if (!TARGET_64BIT)
3416     {
3417       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
3418          make an exception for SSE modes since these require 128bit
3419          alignment.
3420
3421          The handling here differs from field_alignment.  ICC aligns MMX
3422          arguments to 4 byte boundaries, while structure fields are aligned
3423          to 8 byte boundaries.  */
3424       if (!TARGET_SSE)
3425         align = PARM_BOUNDARY;
3426       else if (!type)
3427         {
3428           if (!SSE_REG_MODE_P (mode))
3429             align = PARM_BOUNDARY;
3430         }
3431       else
3432         {
3433           if (!contains_128bit_aligned_vector_p (type))
3434             align = PARM_BOUNDARY;
3435         }
3436     }
3437   if (align > 128)
3438     align = 128;
3439   return align;
3440 }
3441
3442 /* Return true if N is a possible register number of function value.  */
3443 bool
3444 ix86_function_value_regno_p (int regno)
3445 {
3446   if (regno == 0
3447       || (regno == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
3448       || (regno == FIRST_SSE_REG && TARGET_SSE))
3449     return true;
3450
3451   if (!TARGET_64BIT
3452       && (regno == FIRST_MMX_REG && TARGET_MMX))
3453         return true;
3454
3455   return false;
3456 }
3457
3458 /* Define how to find the value returned by a function.
3459    VALTYPE is the data type of the value (as a tree).
3460    If the precise function being called is known, FUNC is its FUNCTION_DECL;
3461    otherwise, FUNC is 0.  */
3462 rtx
3463 ix86_function_value (tree valtype, tree fntype_or_decl,
3464                      bool outgoing ATTRIBUTE_UNUSED)
3465 {
3466   enum machine_mode natmode = type_natural_mode (valtype);
3467
3468   if (TARGET_64BIT)
3469     {
3470       rtx ret = construct_container (natmode, TYPE_MODE (valtype), valtype,
3471                                      1, REGPARM_MAX, SSE_REGPARM_MAX,
3472                                      x86_64_int_return_registers, 0);
3473       /* For zero sized structures, construct_container return NULL, but we
3474          need to keep rest of compiler happy by returning meaningful value.  */
3475       if (!ret)
3476         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
3477       return ret;
3478     }
3479   else
3480     {
3481       tree fn = NULL_TREE, fntype;
3482       if (fntype_or_decl
3483           && DECL_P (fntype_or_decl))
3484         fn = fntype_or_decl;
3485       fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
3486       return gen_rtx_REG (TYPE_MODE (valtype),
3487                           ix86_value_regno (natmode, fn, fntype));
3488     }
3489 }
3490
3491 /* Return false iff type is returned in memory.  */
3492 int
3493 ix86_return_in_memory (tree type)
3494 {
3495   int needed_intregs, needed_sseregs, size;
3496   enum machine_mode mode = type_natural_mode (type);
3497
3498   if (TARGET_64BIT)
3499     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
3500
3501   if (mode == BLKmode)
3502     return 1;
3503
3504   size = int_size_in_bytes (type);
3505
3506   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
3507     return 0;
3508
3509   if (VECTOR_MODE_P (mode) || mode == TImode)
3510     {
3511       /* User-created vectors small enough to fit in EAX.  */
3512       if (size < 8)
3513         return 0;
3514
3515       /* MMX/3dNow values are returned in MM0,
3516          except when it doesn't exits.  */
3517       if (size == 8)
3518         return (TARGET_MMX ? 0 : 1);
3519
3520       /* SSE values are returned in XMM0, except when it doesn't exist.  */
3521       if (size == 16)
3522         return (TARGET_SSE ? 0 : 1);
3523     }
3524
3525   if (mode == XFmode)
3526     return 0;
3527
3528   if (size > 12)
3529     return 1;
3530   return 0;
3531 }
3532
3533 /* When returning SSE vector types, we have a choice of either
3534      (1) being abi incompatible with a -march switch, or
3535      (2) generating an error.
3536    Given no good solution, I think the safest thing is one warning.
3537    The user won't be able to use -Werror, but....
3538
3539    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
3540    called in response to actually generating a caller or callee that
3541    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
3542    via aggregate_value_p for general type probing from tree-ssa.  */
3543
3544 static rtx
3545 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
3546 {
3547   static bool warnedsse, warnedmmx;
3548
3549   if (type)
3550     {
3551       /* Look at the return type of the function, not the function type.  */
3552       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
3553
3554       if (!TARGET_SSE && !warnedsse)
3555         {
3556           if (mode == TImode
3557               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3558             {
3559               warnedsse = true;
3560               warning (0, "SSE vector return without SSE enabled "
3561                        "changes the ABI");
3562             }
3563         }
3564
3565       if (!TARGET_MMX && !warnedmmx)
3566         {
3567           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
3568             {
3569               warnedmmx = true;
3570               warning (0, "MMX vector return without MMX enabled "
3571                        "changes the ABI");
3572             }
3573         }
3574     }
3575
3576   return NULL;
3577 }
3578
3579 /* Define how to find the value returned by a library function
3580    assuming the value has mode MODE.  */
3581 rtx
3582 ix86_libcall_value (enum machine_mode mode)
3583 {
3584   if (TARGET_64BIT)
3585     {
3586       switch (mode)
3587         {
3588         case SFmode:
3589         case SCmode:
3590         case DFmode:
3591         case DCmode:
3592         case TFmode:
3593           return gen_rtx_REG (mode, FIRST_SSE_REG);
3594         case XFmode:
3595         case XCmode:
3596           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
3597         case TCmode:
3598           return NULL;
3599         default:
3600           return gen_rtx_REG (mode, 0);
3601         }
3602     }
3603   else
3604     return gen_rtx_REG (mode, ix86_value_regno (mode, NULL, NULL));
3605 }
3606
3607 /* Given a mode, return the register to use for a return value.  */
3608
3609 static int
3610 ix86_value_regno (enum machine_mode mode, tree func, tree fntype)
3611 {
3612   gcc_assert (!TARGET_64BIT);
3613
3614   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
3615      we prevent this case when mmx is not available.  */
3616   if ((VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8))
3617     return FIRST_MMX_REG;
3618
3619   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
3620      we prevent this case when sse is not available.  */
3621   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3622     return FIRST_SSE_REG;
3623
3624   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
3625   if (!SCALAR_FLOAT_MODE_P (mode) || !TARGET_FLOAT_RETURNS_IN_80387)
3626     return 0;
3627
3628   /* Floating point return values in %st(0), except for local functions when
3629      SSE math is enabled or for functions with sseregparm attribute.  */
3630   if ((func || fntype)
3631       && (mode == SFmode || mode == DFmode))
3632     {
3633       int sse_level = ix86_function_sseregparm (fntype, func);
3634       if ((sse_level >= 1 && mode == SFmode)
3635           || (sse_level == 2 && mode == DFmode))
3636         return FIRST_SSE_REG;
3637     }
3638
3639   return FIRST_FLOAT_REG;
3640 }
3641 \f
3642 /* Create the va_list data type.  */
3643
3644 static tree
3645 ix86_build_builtin_va_list (void)
3646 {
3647   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
3648
3649   /* For i386 we use plain pointer to argument area.  */
3650   if (!TARGET_64BIT)
3651     return build_pointer_type (char_type_node);
3652
3653   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3654   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
3655
3656   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
3657                       unsigned_type_node);
3658   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
3659                       unsigned_type_node);
3660   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
3661                       ptr_type_node);
3662   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
3663                       ptr_type_node);
3664
3665   va_list_gpr_counter_field = f_gpr;
3666   va_list_fpr_counter_field = f_fpr;
3667
3668   DECL_FIELD_CONTEXT (f_gpr) = record;
3669   DECL_FIELD_CONTEXT (f_fpr) = record;
3670   DECL_FIELD_CONTEXT (f_ovf) = record;
3671   DECL_FIELD_CONTEXT (f_sav) = record;
3672
3673   TREE_CHAIN (record) = type_decl;
3674   TYPE_NAME (record) = type_decl;
3675   TYPE_FIELDS (record) = f_gpr;
3676   TREE_CHAIN (f_gpr) = f_fpr;
3677   TREE_CHAIN (f_fpr) = f_ovf;
3678   TREE_CHAIN (f_ovf) = f_sav;
3679
3680   layout_type (record);
3681
3682   /* The correct type is an array type of one element.  */
3683   return build_array_type (record, build_index_type (size_zero_node));
3684 }
3685
3686 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
3687
3688 static void
3689 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3690                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
3691                              int no_rtl)
3692 {
3693   CUMULATIVE_ARGS next_cum;
3694   rtx save_area = NULL_RTX, mem;
3695   rtx label;
3696   rtx label_ref;
3697   rtx tmp_reg;
3698   rtx nsse_reg;
3699   int set;
3700   tree fntype;
3701   int stdarg_p;
3702   int i;
3703
3704   if (!TARGET_64BIT)
3705     return;
3706
3707   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
3708     return;
3709
3710   /* Indicate to allocate space on the stack for varargs save area.  */
3711   ix86_save_varrargs_registers = 1;
3712
3713   cfun->stack_alignment_needed = 128;
3714
3715   fntype = TREE_TYPE (current_function_decl);
3716   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
3717               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3718                   != void_type_node));
3719
3720   /* For varargs, we do not want to skip the dummy va_dcl argument.
3721      For stdargs, we do want to skip the last named argument.  */
3722   next_cum = *cum;
3723   if (stdarg_p)
3724     function_arg_advance (&next_cum, mode, type, 1);
3725
3726   if (!no_rtl)
3727     save_area = frame_pointer_rtx;
3728
3729   set = get_varargs_alias_set ();
3730
3731   for (i = next_cum.regno;
3732        i < ix86_regparm
3733        && i < next_cum.regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
3734        i++)
3735     {
3736       mem = gen_rtx_MEM (Pmode,
3737                          plus_constant (save_area, i * UNITS_PER_WORD));
3738       MEM_NOTRAP_P (mem) = 1;
3739       set_mem_alias_set (mem, set);
3740       emit_move_insn (mem, gen_rtx_REG (Pmode,
3741                                         x86_64_int_parameter_registers[i]));
3742     }
3743
3744   if (next_cum.sse_nregs && cfun->va_list_fpr_size)
3745     {
3746       /* Now emit code to save SSE registers.  The AX parameter contains number
3747          of SSE parameter registers used to call this function.  We use
3748          sse_prologue_save insn template that produces computed jump across
3749          SSE saves.  We need some preparation work to get this working.  */
3750
3751       label = gen_label_rtx ();
3752       label_ref = gen_rtx_LABEL_REF (Pmode, label);
3753
3754       /* Compute address to jump to :
3755          label - 5*eax + nnamed_sse_arguments*5  */
3756       tmp_reg = gen_reg_rtx (Pmode);
3757       nsse_reg = gen_reg_rtx (Pmode);
3758       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
3759       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3760                               gen_rtx_MULT (Pmode, nsse_reg,
3761                                             GEN_INT (4))));
3762       if (next_cum.sse_regno)
3763         emit_move_insn
3764           (nsse_reg,
3765            gen_rtx_CONST (DImode,
3766                           gen_rtx_PLUS (DImode,
3767                                         label_ref,
3768                                         GEN_INT (next_cum.sse_regno * 4))));
3769       else
3770         emit_move_insn (nsse_reg, label_ref);
3771       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
3772
3773       /* Compute address of memory block we save into.  We always use pointer
3774          pointing 127 bytes after first byte to store - this is needed to keep
3775          instruction size limited by 4 bytes.  */
3776       tmp_reg = gen_reg_rtx (Pmode);
3777       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3778                               plus_constant (save_area,
3779                                              8 * REGPARM_MAX + 127)));
3780       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
3781       MEM_NOTRAP_P (mem) = 1;
3782       set_mem_alias_set (mem, set);
3783       set_mem_align (mem, BITS_PER_WORD);
3784
3785       /* And finally do the dirty job!  */
3786       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
3787                                         GEN_INT (next_cum.sse_regno), label));
3788     }
3789
3790 }
3791
3792 /* Implement va_start.  */
3793
3794 void
3795 ix86_va_start (tree valist, rtx nextarg)
3796 {
3797   HOST_WIDE_INT words, n_gpr, n_fpr;
3798   tree f_gpr, f_fpr, f_ovf, f_sav;
3799   tree gpr, fpr, ovf, sav, t;
3800
3801   /* Only 64bit target needs something special.  */
3802   if (!TARGET_64BIT)
3803     {
3804       std_expand_builtin_va_start (valist, nextarg);
3805       return;
3806     }
3807
3808   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3809   f_fpr = TREE_CHAIN (f_gpr);
3810   f_ovf = TREE_CHAIN (f_fpr);
3811   f_sav = TREE_CHAIN (f_ovf);
3812
3813   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3814   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
3815   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
3816   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
3817   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
3818
3819   /* Count number of gp and fp argument registers used.  */
3820   words = current_function_args_info.words;
3821   n_gpr = current_function_args_info.regno;
3822   n_fpr = current_function_args_info.sse_regno;
3823
3824   if (TARGET_DEBUG_ARG)
3825     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
3826              (int) words, (int) n_gpr, (int) n_fpr);
3827
3828   if (cfun->va_list_gpr_size)
3829     {
3830       t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
3831                   build_int_cst (NULL_TREE, n_gpr * 8));
3832       TREE_SIDE_EFFECTS (t) = 1;
3833       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3834     }
3835
3836   if (cfun->va_list_fpr_size)
3837     {
3838       t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
3839                   build_int_cst (NULL_TREE, n_fpr * 16 + 8*REGPARM_MAX));
3840       TREE_SIDE_EFFECTS (t) = 1;
3841       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3842     }
3843
3844   /* Find the overflow area.  */
3845   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
3846   if (words != 0)
3847     t = build2 (PLUS_EXPR, TREE_TYPE (ovf), t,
3848                 build_int_cst (NULL_TREE, words * UNITS_PER_WORD));
3849   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3850   TREE_SIDE_EFFECTS (t) = 1;
3851   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3852
3853   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
3854     {
3855       /* Find the register save area.
3856          Prologue of the function save it right above stack frame.  */
3857       t = make_tree (TREE_TYPE (sav), frame_pointer_rtx);
3858       t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
3859       TREE_SIDE_EFFECTS (t) = 1;
3860       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3861     }
3862 }
3863
3864 /* Implement va_arg.  */
3865
3866 tree
3867 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
3868 {
3869   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
3870   tree f_gpr, f_fpr, f_ovf, f_sav;
3871   tree gpr, fpr, ovf, sav, t;
3872   int size, rsize;
3873   tree lab_false, lab_over = NULL_TREE;
3874   tree addr, t2;
3875   rtx container;
3876   int indirect_p = 0;
3877   tree ptrtype;
3878   enum machine_mode nat_mode;
3879
3880   /* Only 64bit target needs something special.  */
3881   if (!TARGET_64BIT)
3882     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
3883
3884   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3885   f_fpr = TREE_CHAIN (f_gpr);
3886   f_ovf = TREE_CHAIN (f_fpr);
3887   f_sav = TREE_CHAIN (f_ovf);
3888
3889   valist = build_va_arg_indirect_ref (valist);
3890   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
3891   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
3892   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
3893   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
3894
3895   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3896   if (indirect_p)
3897     type = build_pointer_type (type);
3898   size = int_size_in_bytes (type);
3899   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3900
3901   nat_mode = type_natural_mode (type);
3902   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
3903                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
3904
3905   /* Pull the value out of the saved registers.  */
3906
3907   addr = create_tmp_var (ptr_type_node, "addr");
3908   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
3909
3910   if (container)
3911     {
3912       int needed_intregs, needed_sseregs;
3913       bool need_temp;
3914       tree int_addr, sse_addr;
3915
3916       lab_false = create_artificial_label ();
3917       lab_over = create_artificial_label ();
3918
3919       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
3920
3921       need_temp = (!REG_P (container)
3922                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
3923                        || TYPE_ALIGN (type) > 128));
3924
3925       /* In case we are passing structure, verify that it is consecutive block
3926          on the register save area.  If not we need to do moves.  */
3927       if (!need_temp && !REG_P (container))
3928         {
3929           /* Verify that all registers are strictly consecutive  */
3930           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
3931             {
3932               int i;
3933
3934               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3935                 {
3936                   rtx slot = XVECEXP (container, 0, i);
3937                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
3938                       || INTVAL (XEXP (slot, 1)) != i * 16)
3939                     need_temp = 1;
3940                 }
3941             }
3942           else
3943             {
3944               int i;
3945
3946               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3947                 {
3948                   rtx slot = XVECEXP (container, 0, i);
3949                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
3950                       || INTVAL (XEXP (slot, 1)) != i * 8)
3951                     need_temp = 1;
3952                 }
3953             }
3954         }
3955       if (!need_temp)
3956         {
3957           int_addr = addr;
3958           sse_addr = addr;
3959         }
3960       else
3961         {
3962           int_addr = create_tmp_var (ptr_type_node, "int_addr");
3963           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
3964           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
3965           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
3966         }
3967
3968       /* First ensure that we fit completely in registers.  */
3969       if (needed_intregs)
3970         {
3971           t = build_int_cst (TREE_TYPE (gpr),
3972                              (REGPARM_MAX - needed_intregs + 1) * 8);
3973           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
3974           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
3975           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
3976           gimplify_and_add (t, pre_p);
3977         }
3978       if (needed_sseregs)
3979         {
3980           t = build_int_cst (TREE_TYPE (fpr),
3981                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
3982                              + REGPARM_MAX * 8);
3983           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
3984           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
3985           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
3986           gimplify_and_add (t, pre_p);
3987         }
3988
3989       /* Compute index to start of area used for integer regs.  */
3990       if (needed_intregs)
3991         {
3992           /* int_addr = gpr + sav; */
3993           t = fold_convert (ptr_type_node, gpr);
3994           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
3995           t = build2 (MODIFY_EXPR, void_type_node, int_addr, t);
3996           gimplify_and_add (t, pre_p);
3997         }
3998       if (needed_sseregs)
3999         {
4000           /* sse_addr = fpr + sav; */
4001           t = fold_convert (ptr_type_node, fpr);
4002           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4003           t = build2 (MODIFY_EXPR, void_type_node, sse_addr, t);
4004           gimplify_and_add (t, pre_p);
4005         }
4006       if (need_temp)
4007         {
4008           int i;
4009           tree temp = create_tmp_var (type, "va_arg_tmp");
4010
4011           /* addr = &temp; */
4012           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4013           t = build2 (MODIFY_EXPR, void_type_node, addr, t);
4014           gimplify_and_add (t, pre_p);
4015
4016           for (i = 0; i < XVECLEN (container, 0); i++)
4017             {
4018               rtx slot = XVECEXP (container, 0, i);
4019               rtx reg = XEXP (slot, 0);
4020               enum machine_mode mode = GET_MODE (reg);
4021               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4022               tree addr_type = build_pointer_type (piece_type);
4023               tree src_addr, src;
4024               int src_offset;
4025               tree dest_addr, dest;
4026
4027               if (SSE_REGNO_P (REGNO (reg)))
4028                 {
4029                   src_addr = sse_addr;
4030                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4031                 }
4032               else
4033                 {
4034                   src_addr = int_addr;
4035                   src_offset = REGNO (reg) * 8;
4036                 }
4037               src_addr = fold_convert (addr_type, src_addr);
4038               src_addr = fold (build2 (PLUS_EXPR, addr_type, src_addr,
4039                                        size_int (src_offset)));
4040               src = build_va_arg_indirect_ref (src_addr);
4041
4042               dest_addr = fold_convert (addr_type, addr);
4043               dest_addr = fold (build2 (PLUS_EXPR, addr_type, dest_addr,
4044                                         size_int (INTVAL (XEXP (slot, 1)))));
4045               dest = build_va_arg_indirect_ref (dest_addr);
4046
4047               t = build2 (MODIFY_EXPR, void_type_node, dest, src);
4048               gimplify_and_add (t, pre_p);
4049             }
4050         }
4051
4052       if (needed_intregs)
4053         {
4054           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4055                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4056           t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4057           gimplify_and_add (t, pre_p);
4058         }
4059       if (needed_sseregs)
4060         {
4061           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4062                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4063           t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
4064           gimplify_and_add (t, pre_p);
4065         }
4066
4067       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4068       gimplify_and_add (t, pre_p);
4069
4070       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4071       append_to_statement_list (t, pre_p);
4072     }
4073
4074   /* ... otherwise out of the overflow area.  */
4075
4076   /* Care for on-stack alignment if needed.  */
4077   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64)
4078     t = ovf;
4079   else
4080     {
4081       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4082       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4083                   build_int_cst (TREE_TYPE (ovf), align - 1));
4084       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4085                   build_int_cst (TREE_TYPE (t), -align));
4086     }
4087   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4088
4089   t2 = build2 (MODIFY_EXPR, void_type_node, addr, t);
4090   gimplify_and_add (t2, pre_p);
4091
4092   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4093               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4094   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4095   gimplify_and_add (t, pre_p);
4096
4097   if (container)
4098     {
4099       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4100       append_to_statement_list (t, pre_p);
4101     }
4102
4103   ptrtype = build_pointer_type (type);
4104   addr = fold_convert (ptrtype, addr);
4105
4106   if (indirect_p)
4107     addr = build_va_arg_indirect_ref (addr);
4108   return build_va_arg_indirect_ref (addr);
4109 }
4110 \f
4111 /* Return nonzero if OPNUM's MEM should be matched
4112    in movabs* patterns.  */
4113
4114 int
4115 ix86_check_movabs (rtx insn, int opnum)
4116 {
4117   rtx set, mem;
4118
4119   set = PATTERN (insn);
4120   if (GET_CODE (set) == PARALLEL)
4121     set = XVECEXP (set, 0, 0);
4122   gcc_assert (GET_CODE (set) == SET);
4123   mem = XEXP (set, opnum);
4124   while (GET_CODE (mem) == SUBREG)
4125     mem = SUBREG_REG (mem);
4126   gcc_assert (GET_CODE (mem) == MEM);
4127   return (volatile_ok || !MEM_VOLATILE_P (mem));
4128 }
4129 \f
4130 /* Initialize the table of extra 80387 mathematical constants.  */
4131
4132 static void
4133 init_ext_80387_constants (void)
4134 {
4135   static const char * cst[5] =
4136   {
4137     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4138     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4139     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4140     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4141     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4142   };
4143   int i;
4144
4145   for (i = 0; i < 5; i++)
4146     {
4147       real_from_string (&ext_80387_constants_table[i], cst[i]);
4148       /* Ensure each constant is rounded to XFmode precision.  */
4149       real_convert (&ext_80387_constants_table[i],
4150                     XFmode, &ext_80387_constants_table[i]);
4151     }
4152
4153   ext_80387_constants_init = 1;
4154 }
4155
4156 /* Return true if the constant is something that can be loaded with
4157    a special instruction.  */
4158
4159 int
4160 standard_80387_constant_p (rtx x)
4161 {
4162   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4163     return -1;
4164
4165   if (x == CONST0_RTX (GET_MODE (x)))
4166     return 1;
4167   if (x == CONST1_RTX (GET_MODE (x)))
4168     return 2;
4169
4170   /* For XFmode constants, try to find a special 80387 instruction when
4171      optimizing for size or on those CPUs that benefit from them.  */
4172   if (GET_MODE (x) == XFmode
4173       && (optimize_size || x86_ext_80387_constants & TUNEMASK))
4174     {
4175       REAL_VALUE_TYPE r;
4176       int i;
4177
4178       if (! ext_80387_constants_init)
4179         init_ext_80387_constants ();
4180
4181       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4182       for (i = 0; i < 5; i++)
4183         if (real_identical (&r, &ext_80387_constants_table[i]))
4184           return i + 3;
4185     }
4186
4187   return 0;
4188 }
4189
4190 /* Return the opcode of the special instruction to be used to load
4191    the constant X.  */
4192
4193 const char *
4194 standard_80387_constant_opcode (rtx x)
4195 {
4196   switch (standard_80387_constant_p (x))
4197     {
4198     case 1:
4199       return "fldz";
4200     case 2:
4201       return "fld1";
4202     case 3:
4203       return "fldlg2";
4204     case 4:
4205       return "fldln2";
4206     case 5:
4207       return "fldl2e";
4208     case 6:
4209       return "fldl2t";
4210     case 7:
4211       return "fldpi";
4212     default:
4213       gcc_unreachable ();
4214     }
4215 }
4216
4217 /* Return the CONST_DOUBLE representing the 80387 constant that is
4218    loaded by the specified special instruction.  The argument IDX
4219    matches the return value from standard_80387_constant_p.  */
4220
4221 rtx
4222 standard_80387_constant_rtx (int idx)
4223 {
4224   int i;
4225
4226   if (! ext_80387_constants_init)
4227     init_ext_80387_constants ();
4228
4229   switch (idx)
4230     {
4231     case 3:
4232     case 4:
4233     case 5:
4234     case 6:
4235     case 7:
4236       i = idx - 3;
4237       break;
4238
4239     default:
4240       gcc_unreachable ();
4241     }
4242
4243   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
4244                                        XFmode);
4245 }
4246
4247 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4248  */
4249 int
4250 standard_sse_constant_p (rtx x)
4251 {
4252   if (x == const0_rtx)
4253     return 1;
4254   return (x == CONST0_RTX (GET_MODE (x)));
4255 }
4256
4257 /* Returns 1 if OP contains a symbol reference */
4258
4259 int
4260 symbolic_reference_mentioned_p (rtx op)
4261 {
4262   const char *fmt;
4263   int i;
4264
4265   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4266     return 1;
4267
4268   fmt = GET_RTX_FORMAT (GET_CODE (op));
4269   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4270     {
4271       if (fmt[i] == 'E')
4272         {
4273           int j;
4274
4275           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4276             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4277               return 1;
4278         }
4279
4280       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4281         return 1;
4282     }
4283
4284   return 0;
4285 }
4286
4287 /* Return 1 if it is appropriate to emit `ret' instructions in the
4288    body of a function.  Do this only if the epilogue is simple, needing a
4289    couple of insns.  Prior to reloading, we can't tell how many registers
4290    must be saved, so return 0 then.  Return 0 if there is no frame
4291    marker to de-allocate.  */
4292
4293 int
4294 ix86_can_use_return_insn_p (void)
4295 {
4296   struct ix86_frame frame;
4297
4298   if (! reload_completed || frame_pointer_needed)
4299     return 0;
4300
4301   /* Don't allow more than 32 pop, since that's all we can do
4302      with one instruction.  */
4303   if (current_function_pops_args
4304       && current_function_args_size >= 32768)
4305     return 0;
4306
4307   ix86_compute_frame_layout (&frame);
4308   return frame.to_allocate == 0 && frame.nregs == 0;
4309 }
4310 \f
4311 /* Value should be nonzero if functions must have frame pointers.
4312    Zero means the frame pointer need not be set up (and parms may
4313    be accessed via the stack pointer) in functions that seem suitable.  */
4314
4315 int
4316 ix86_frame_pointer_required (void)
4317 {
4318   /* If we accessed previous frames, then the generated code expects
4319      to be able to access the saved ebp value in our frame.  */
4320   if (cfun->machine->accesses_prev_frame)
4321     return 1;
4322
4323   /* Several x86 os'es need a frame pointer for other reasons,
4324      usually pertaining to setjmp.  */
4325   if (SUBTARGET_FRAME_POINTER_REQUIRED)
4326     return 1;
4327
4328   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
4329      the frame pointer by default.  Turn it back on now if we've not
4330      got a leaf function.  */
4331   if (TARGET_OMIT_LEAF_FRAME_POINTER
4332       && (!current_function_is_leaf))
4333     return 1;
4334
4335   if (current_function_profile)
4336     return 1;
4337
4338   return 0;
4339 }
4340
4341 /* Record that the current function accesses previous call frames.  */
4342
4343 void
4344 ix86_setup_frame_addresses (void)
4345 {
4346   cfun->machine->accesses_prev_frame = 1;
4347 }
4348 \f
4349 #if defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)
4350 # define USE_HIDDEN_LINKONCE 1
4351 #else
4352 # define USE_HIDDEN_LINKONCE 0
4353 #endif
4354
4355 static int pic_labels_used;
4356
4357 /* Fills in the label name that should be used for a pc thunk for
4358    the given register.  */
4359
4360 static void
4361 get_pc_thunk_name (char name[32], unsigned int regno)
4362 {
4363   if (USE_HIDDEN_LINKONCE)
4364     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
4365   else
4366     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
4367 }
4368
4369
4370 /* This function generates code for -fpic that loads %ebx with
4371    the return address of the caller and then returns.  */
4372
4373 void
4374 ix86_file_end (void)
4375 {
4376   rtx xops[2];
4377   int regno;
4378
4379   for (regno = 0; regno < 8; ++regno)
4380     {
4381       char name[32];
4382
4383       if (! ((pic_labels_used >> regno) & 1))
4384         continue;
4385
4386       get_pc_thunk_name (name, regno);
4387
4388       if (USE_HIDDEN_LINKONCE)
4389         {
4390           tree decl;
4391
4392           decl = build_decl (FUNCTION_DECL, get_identifier (name),
4393                              error_mark_node);
4394           TREE_PUBLIC (decl) = 1;
4395           TREE_STATIC (decl) = 1;
4396           DECL_ONE_ONLY (decl) = 1;
4397
4398           (*targetm.asm_out.unique_section) (decl, 0);
4399           switch_to_section (get_named_section (decl, NULL, 0));
4400
4401           (*targetm.asm_out.globalize_label) (asm_out_file, name);
4402           fputs ("\t.hidden\t", asm_out_file);
4403           assemble_name (asm_out_file, name);
4404           fputc ('\n', asm_out_file);
4405           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
4406         }
4407       else
4408         {
4409           switch_to_section (text_section);
4410           ASM_OUTPUT_LABEL (asm_out_file, name);
4411         }
4412
4413       xops[0] = gen_rtx_REG (SImode, regno);
4414       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
4415       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
4416       output_asm_insn ("ret", xops);
4417     }
4418
4419   if (NEED_INDICATE_EXEC_STACK)
4420     file_end_indicate_exec_stack ();
4421 }
4422
4423 /* Emit code for the SET_GOT patterns.  */
4424
4425 const char *
4426 output_set_got (rtx dest)
4427 {
4428   rtx xops[3];
4429
4430   xops[0] = dest;
4431   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
4432
4433   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
4434     {
4435       xops[2] = gen_rtx_LABEL_REF (Pmode, gen_label_rtx ());
4436
4437       if (!flag_pic)
4438         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
4439       else
4440         output_asm_insn ("call\t%a2", xops);
4441
4442 #if TARGET_MACHO
4443       /* Output the "canonical" label name ("Lxx$pb") here too.  This
4444          is what will be referred to by the Mach-O PIC subsystem.  */
4445       ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4446 #endif
4447       (*targetm.asm_out.internal_label) (asm_out_file, "L",
4448                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
4449
4450       if (flag_pic)
4451         output_asm_insn ("pop{l}\t%0", xops);
4452     }
4453   else
4454     {
4455       char name[32];
4456       get_pc_thunk_name (name, REGNO (dest));
4457       pic_labels_used |= 1 << REGNO (dest);
4458
4459       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4460       xops[2] = gen_rtx_MEM (QImode, xops[2]);
4461       output_asm_insn ("call\t%X2", xops);
4462     }
4463
4464   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
4465     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
4466   else if (!TARGET_MACHO)
4467     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
4468
4469   return "";
4470 }
4471
4472 /* Generate an "push" pattern for input ARG.  */
4473
4474 static rtx
4475 gen_push (rtx arg)
4476 {
4477   return gen_rtx_SET (VOIDmode,
4478                       gen_rtx_MEM (Pmode,
4479                                    gen_rtx_PRE_DEC (Pmode,
4480                                                     stack_pointer_rtx)),
4481                       arg);
4482 }
4483
4484 /* Return >= 0 if there is an unused call-clobbered register available
4485    for the entire function.  */
4486
4487 static unsigned int
4488 ix86_select_alt_pic_regnum (void)
4489 {
4490   if (current_function_is_leaf && !current_function_profile)
4491     {
4492       int i;
4493       for (i = 2; i >= 0; --i)
4494         if (!regs_ever_live[i])
4495           return i;
4496     }
4497
4498   return INVALID_REGNUM;
4499 }
4500
4501 /* Return 1 if we need to save REGNO.  */
4502 static int
4503 ix86_save_reg (unsigned int regno, int maybe_eh_return)
4504 {
4505   if (pic_offset_table_rtx
4506       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
4507       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4508           || current_function_profile
4509           || current_function_calls_eh_return
4510           || current_function_uses_const_pool))
4511     {
4512       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
4513         return 0;
4514       return 1;
4515     }
4516
4517   if (current_function_calls_eh_return && maybe_eh_return)
4518     {
4519       unsigned i;
4520       for (i = 0; ; i++)
4521         {
4522           unsigned test = EH_RETURN_DATA_REGNO (i);
4523           if (test == INVALID_REGNUM)
4524             break;
4525           if (test == regno)
4526             return 1;
4527         }
4528     }
4529
4530   if (cfun->machine->force_align_arg_pointer
4531       && regno == REGNO (cfun->machine->force_align_arg_pointer))
4532     return 1;
4533
4534   return (regs_ever_live[regno]
4535           && !call_used_regs[regno]
4536           && !fixed_regs[regno]
4537           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
4538 }
4539
4540 /* Return number of registers to be saved on the stack.  */
4541
4542 static int
4543 ix86_nsaved_regs (void)
4544 {
4545   int nregs = 0;
4546   int regno;
4547
4548   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4549     if (ix86_save_reg (regno, true))
4550       nregs++;
4551   return nregs;
4552 }
4553
4554 /* Return the offset between two registers, one to be eliminated, and the other
4555    its replacement, at the start of a routine.  */
4556
4557 HOST_WIDE_INT
4558 ix86_initial_elimination_offset (int from, int to)
4559 {
4560   struct ix86_frame frame;
4561   ix86_compute_frame_layout (&frame);
4562
4563   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
4564     return frame.hard_frame_pointer_offset;
4565   else if (from == FRAME_POINTER_REGNUM
4566            && to == HARD_FRAME_POINTER_REGNUM)
4567     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
4568   else
4569     {
4570       gcc_assert (to == STACK_POINTER_REGNUM);
4571
4572       if (from == ARG_POINTER_REGNUM)
4573         return frame.stack_pointer_offset;
4574       
4575       gcc_assert (from == FRAME_POINTER_REGNUM);
4576       return frame.stack_pointer_offset - frame.frame_pointer_offset;
4577     }
4578 }
4579
4580 /* Fill structure ix86_frame about frame of currently computed function.  */
4581
4582 static void
4583 ix86_compute_frame_layout (struct ix86_frame *frame)
4584 {
4585   HOST_WIDE_INT total_size;
4586   unsigned int stack_alignment_needed;
4587   HOST_WIDE_INT offset;
4588   unsigned int preferred_alignment;
4589   HOST_WIDE_INT size = get_frame_size ();
4590
4591   frame->nregs = ix86_nsaved_regs ();
4592   total_size = size;
4593
4594   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
4595   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
4596
4597   /* During reload iteration the amount of registers saved can change.
4598      Recompute the value as needed.  Do not recompute when amount of registers
4599      didn't change as reload does multiple calls to the function and does not
4600      expect the decision to change within single iteration.  */
4601   if (!optimize_size
4602       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
4603     {
4604       int count = frame->nregs;
4605
4606       cfun->machine->use_fast_prologue_epilogue_nregs = count;
4607       /* The fast prologue uses move instead of push to save registers.  This
4608          is significantly longer, but also executes faster as modern hardware
4609          can execute the moves in parallel, but can't do that for push/pop.
4610
4611          Be careful about choosing what prologue to emit:  When function takes
4612          many instructions to execute we may use slow version as well as in
4613          case function is known to be outside hot spot (this is known with
4614          feedback only).  Weight the size of function by number of registers
4615          to save as it is cheap to use one or two push instructions but very
4616          slow to use many of them.  */
4617       if (count)
4618         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
4619       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
4620           || (flag_branch_probabilities
4621               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
4622         cfun->machine->use_fast_prologue_epilogue = false;
4623       else
4624         cfun->machine->use_fast_prologue_epilogue
4625            = !expensive_function_p (count);
4626     }
4627   if (TARGET_PROLOGUE_USING_MOVE
4628       && cfun->machine->use_fast_prologue_epilogue)
4629     frame->save_regs_using_mov = true;
4630   else
4631     frame->save_regs_using_mov = false;
4632
4633
4634   /* Skip return address and saved base pointer.  */
4635   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
4636
4637   frame->hard_frame_pointer_offset = offset;
4638
4639   /* Do some sanity checking of stack_alignment_needed and
4640      preferred_alignment, since i386 port is the only using those features
4641      that may break easily.  */
4642
4643   gcc_assert (!size || stack_alignment_needed);
4644   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
4645   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
4646   gcc_assert (stack_alignment_needed
4647               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
4648
4649   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
4650     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
4651
4652   /* Register save area */
4653   offset += frame->nregs * UNITS_PER_WORD;
4654
4655   /* Va-arg area */
4656   if (ix86_save_varrargs_registers)
4657     {
4658       offset += X86_64_VARARGS_SIZE;
4659       frame->va_arg_size = X86_64_VARARGS_SIZE;
4660     }
4661   else
4662     frame->va_arg_size = 0;
4663
4664   /* Align start of frame for local function.  */
4665   frame->padding1 = ((offset + stack_alignment_needed - 1)
4666                      & -stack_alignment_needed) - offset;
4667
4668   offset += frame->padding1;
4669
4670   /* Frame pointer points here.  */
4671   frame->frame_pointer_offset = offset;
4672
4673   offset += size;
4674
4675   /* Add outgoing arguments area.  Can be skipped if we eliminated
4676      all the function calls as dead code.
4677      Skipping is however impossible when function calls alloca.  Alloca
4678      expander assumes that last current_function_outgoing_args_size
4679      of stack frame are unused.  */
4680   if (ACCUMULATE_OUTGOING_ARGS
4681       && (!current_function_is_leaf || current_function_calls_alloca))
4682     {
4683       offset += current_function_outgoing_args_size;
4684       frame->outgoing_arguments_size = current_function_outgoing_args_size;
4685     }
4686   else
4687     frame->outgoing_arguments_size = 0;
4688
4689   /* Align stack boundary.  Only needed if we're calling another function
4690      or using alloca.  */
4691   if (!current_function_is_leaf || current_function_calls_alloca)
4692     frame->padding2 = ((offset + preferred_alignment - 1)
4693                        & -preferred_alignment) - offset;
4694   else
4695     frame->padding2 = 0;
4696
4697   offset += frame->padding2;
4698
4699   /* We've reached end of stack frame.  */
4700   frame->stack_pointer_offset = offset;
4701
4702   /* Size prologue needs to allocate.  */
4703   frame->to_allocate =
4704     (size + frame->padding1 + frame->padding2
4705      + frame->outgoing_arguments_size + frame->va_arg_size);
4706
4707   if ((!frame->to_allocate && frame->nregs <= 1)
4708       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
4709     frame->save_regs_using_mov = false;
4710
4711   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
4712       && current_function_is_leaf)
4713     {
4714       frame->red_zone_size = frame->to_allocate;
4715       if (frame->save_regs_using_mov)
4716         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
4717       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
4718         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
4719     }
4720   else
4721     frame->red_zone_size = 0;
4722   frame->to_allocate -= frame->red_zone_size;
4723   frame->stack_pointer_offset -= frame->red_zone_size;
4724 #if 0
4725   fprintf (stderr, "nregs: %i\n", frame->nregs);
4726   fprintf (stderr, "size: %i\n", size);
4727   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
4728   fprintf (stderr, "padding1: %i\n", frame->padding1);
4729   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
4730   fprintf (stderr, "padding2: %i\n", frame->padding2);
4731   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
4732   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
4733   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
4734   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
4735            frame->hard_frame_pointer_offset);
4736   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
4737 #endif
4738 }
4739
4740 /* Emit code to save registers in the prologue.  */
4741
4742 static void
4743 ix86_emit_save_regs (void)
4744 {
4745   unsigned int regno;
4746   rtx insn;
4747
4748   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
4749     if (ix86_save_reg (regno, true))
4750       {
4751         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
4752         RTX_FRAME_RELATED_P (insn) = 1;
4753       }
4754 }
4755
4756 /* Emit code to save registers using MOV insns.  First register
4757    is restored from POINTER + OFFSET.  */
4758 static void
4759 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
4760 {
4761   unsigned int regno;
4762   rtx insn;
4763
4764   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4765     if (ix86_save_reg (regno, true))
4766       {
4767         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
4768                                                Pmode, offset),
4769                                gen_rtx_REG (Pmode, regno));
4770         RTX_FRAME_RELATED_P (insn) = 1;
4771         offset += UNITS_PER_WORD;
4772       }
4773 }
4774
4775 /* Expand prologue or epilogue stack adjustment.
4776    The pattern exist to put a dependency on all ebp-based memory accesses.
4777    STYLE should be negative if instructions should be marked as frame related,
4778    zero if %r11 register is live and cannot be freely used and positive
4779    otherwise.  */
4780
4781 static void
4782 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
4783 {
4784   rtx insn;
4785
4786   if (! TARGET_64BIT)
4787     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
4788   else if (x86_64_immediate_operand (offset, DImode))
4789     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
4790   else
4791     {
4792       rtx r11;
4793       /* r11 is used by indirect sibcall return as well, set before the
4794          epilogue and used after the epilogue.  ATM indirect sibcall
4795          shouldn't be used together with huge frame sizes in one
4796          function because of the frame_size check in sibcall.c.  */
4797       gcc_assert (style);
4798       r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
4799       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
4800       if (style < 0)
4801         RTX_FRAME_RELATED_P (insn) = 1;
4802       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
4803                                                                offset));
4804     }
4805   if (style < 0)
4806     RTX_FRAME_RELATED_P (insn) = 1;
4807 }
4808
4809 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
4810
4811 static rtx
4812 ix86_internal_arg_pointer (void)
4813 {
4814   if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
4815       && DECL_NAME (current_function_decl)
4816       && MAIN_NAME_P (DECL_NAME (current_function_decl))
4817       && DECL_FILE_SCOPE_P (current_function_decl))
4818     {
4819       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
4820       return copy_to_reg (cfun->machine->force_align_arg_pointer);
4821     }
4822   else
4823     return virtual_incoming_args_rtx;
4824 }
4825
4826 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
4827    This is called from dwarf2out.c to emit call frame instructions
4828    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
4829 static void
4830 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
4831 {
4832   rtx unspec = SET_SRC (pattern);
4833   gcc_assert (GET_CODE (unspec) == UNSPEC);
4834
4835   switch (index)
4836     {
4837     case UNSPEC_REG_SAVE:
4838       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
4839                               SET_DEST (pattern));
4840       break;
4841     case UNSPEC_DEF_CFA:
4842       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
4843                          INTVAL (XVECEXP (unspec, 0, 0)));
4844       break;
4845     default:
4846       gcc_unreachable ();
4847     }
4848 }
4849
4850 /* Expand the prologue into a bunch of separate insns.  */
4851
4852 void
4853 ix86_expand_prologue (void)
4854 {
4855   rtx insn;
4856   bool pic_reg_used;
4857   struct ix86_frame frame;
4858   HOST_WIDE_INT allocate;
4859
4860   ix86_compute_frame_layout (&frame);
4861
4862   if (cfun->machine->force_align_arg_pointer)
4863     {
4864       rtx x, y;
4865
4866       /* Grab the argument pointer.  */
4867       x = plus_constant (stack_pointer_rtx, 4);
4868       y = cfun->machine->force_align_arg_pointer;
4869       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
4870       RTX_FRAME_RELATED_P (insn) = 1;
4871
4872       /* The unwind info consists of two parts: install the fafp as the cfa,
4873          and record the fafp as the "save register" of the stack pointer.
4874          The later is there in order that the unwinder can see where it
4875          should restore the stack pointer across the and insn.  */
4876       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
4877       x = gen_rtx_SET (VOIDmode, y, x);
4878       RTX_FRAME_RELATED_P (x) = 1;
4879       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
4880                           UNSPEC_REG_SAVE);
4881       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
4882       RTX_FRAME_RELATED_P (y) = 1;
4883       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
4884       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
4885       REG_NOTES (insn) = x;
4886
4887       /* Align the stack.  */
4888       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
4889                              GEN_INT (-16)));
4890
4891       /* And here we cheat like madmen with the unwind info.  We force the
4892          cfa register back to sp+4, which is exactly what it was at the
4893          start of the function.  Re-pushing the return address results in
4894          the return at the same spot relative to the cfa, and thus is 
4895          correct wrt the unwind info.  */
4896       x = cfun->machine->force_align_arg_pointer;
4897       x = gen_frame_mem (Pmode, plus_constant (x, -4));
4898       insn = emit_insn (gen_push (x));
4899       RTX_FRAME_RELATED_P (insn) = 1;
4900
4901       x = GEN_INT (4);
4902       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
4903       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
4904       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
4905       REG_NOTES (insn) = x;
4906     }
4907
4908   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
4909      slower on all targets.  Also sdb doesn't like it.  */
4910
4911   if (frame_pointer_needed)
4912     {
4913       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
4914       RTX_FRAME_RELATED_P (insn) = 1;
4915
4916       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
4917       RTX_FRAME_RELATED_P (insn) = 1;
4918     }
4919
4920   allocate = frame.to_allocate;
4921
4922   if (!frame.save_regs_using_mov)
4923     ix86_emit_save_regs ();
4924   else
4925     allocate += frame.nregs * UNITS_PER_WORD;
4926
4927   /* When using red zone we may start register saving before allocating
4928      the stack frame saving one cycle of the prologue.  */
4929   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
4930     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
4931                                    : stack_pointer_rtx,
4932                                    -frame.nregs * UNITS_PER_WORD);
4933
4934   if (allocate == 0)
4935     ;
4936   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
4937     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
4938                                GEN_INT (-allocate), -1);
4939   else
4940     {
4941       /* Only valid for Win32.  */
4942       rtx eax = gen_rtx_REG (SImode, 0);
4943       bool eax_live = ix86_eax_live_at_start_p ();
4944       rtx t;
4945
4946       gcc_assert (!TARGET_64BIT);
4947
4948       if (eax_live)
4949         {
4950           emit_insn (gen_push (eax));
4951           allocate -= 4;
4952         }
4953
4954       emit_move_insn (eax, GEN_INT (allocate));
4955
4956       insn = emit_insn (gen_allocate_stack_worker (eax));
4957       RTX_FRAME_RELATED_P (insn) = 1;
4958       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
4959       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
4960       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
4961                                             t, REG_NOTES (insn));
4962
4963       if (eax_live)
4964         {
4965           if (frame_pointer_needed)
4966             t = plus_constant (hard_frame_pointer_rtx,
4967                                allocate
4968                                - frame.to_allocate
4969                                - frame.nregs * UNITS_PER_WORD);
4970           else
4971             t = plus_constant (stack_pointer_rtx, allocate);
4972           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
4973         }
4974     }
4975
4976   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
4977     {
4978       if (!frame_pointer_needed || !frame.to_allocate)
4979         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
4980       else
4981         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
4982                                        -frame.nregs * UNITS_PER_WORD);
4983     }
4984
4985   pic_reg_used = false;
4986   if (pic_offset_table_rtx
4987       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4988           || current_function_profile))
4989     {
4990       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
4991
4992       if (alt_pic_reg_used != INVALID_REGNUM)
4993         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
4994
4995       pic_reg_used = true;
4996     }
4997
4998   if (pic_reg_used)
4999     {
5000       if (TARGET_64BIT)
5001         insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5002       else
5003         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5004
5005       /* Even with accurate pre-reload life analysis, we can wind up
5006          deleting all references to the pic register after reload.
5007          Consider if cross-jumping unifies two sides of a branch
5008          controlled by a comparison vs the only read from a global.
5009          In which case, allow the set_got to be deleted, though we're
5010          too late to do anything about the ebx save in the prologue.  */
5011       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5012     }
5013
5014   /* Prevent function calls from be scheduled before the call to mcount.
5015      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5016   if (current_function_profile)
5017     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5018 }
5019
5020 /* Emit code to restore saved registers using MOV insns.  First register
5021    is restored from POINTER + OFFSET.  */
5022 static void
5023 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5024                                   int maybe_eh_return)
5025 {
5026   int regno;
5027   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5028
5029   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5030     if (ix86_save_reg (regno, maybe_eh_return))
5031       {
5032         /* Ensure that adjust_address won't be forced to produce pointer
5033            out of range allowed by x86-64 instruction set.  */
5034         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5035           {
5036             rtx r11;
5037
5038             r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5039             emit_move_insn (r11, GEN_INT (offset));
5040             emit_insn (gen_adddi3 (r11, r11, pointer));
5041             base_address = gen_rtx_MEM (Pmode, r11);
5042             offset = 0;
5043           }
5044         emit_move_insn (gen_rtx_REG (Pmode, regno),
5045                         adjust_address (base_address, Pmode, offset));
5046         offset += UNITS_PER_WORD;
5047       }
5048 }
5049
5050 /* Restore function stack, frame, and registers.  */
5051
5052 void
5053 ix86_expand_epilogue (int style)
5054 {
5055   int regno;
5056   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
5057   struct ix86_frame frame;
5058   HOST_WIDE_INT offset;
5059
5060   ix86_compute_frame_layout (&frame);
5061
5062   /* Calculate start of saved registers relative to ebp.  Special care
5063      must be taken for the normal return case of a function using
5064      eh_return: the eax and edx registers are marked as saved, but not
5065      restored along this path.  */
5066   offset = frame.nregs;
5067   if (current_function_calls_eh_return && style != 2)
5068     offset -= 2;
5069   offset *= -UNITS_PER_WORD;
5070
5071   /* If we're only restoring one register and sp is not valid then
5072      using a move instruction to restore the register since it's
5073      less work than reloading sp and popping the register.
5074
5075      The default code result in stack adjustment using add/lea instruction,
5076      while this code results in LEAVE instruction (or discrete equivalent),
5077      so it is profitable in some other cases as well.  Especially when there
5078      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
5079      and there is exactly one register to pop. This heuristic may need some
5080      tuning in future.  */
5081   if ((!sp_valid && frame.nregs <= 1)
5082       || (TARGET_EPILOGUE_USING_MOVE
5083           && cfun->machine->use_fast_prologue_epilogue
5084           && (frame.nregs > 1 || frame.to_allocate))
5085       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
5086       || (frame_pointer_needed && TARGET_USE_LEAVE
5087           && cfun->machine->use_fast_prologue_epilogue
5088           && frame.nregs == 1)
5089       || current_function_calls_eh_return)
5090     {
5091       /* Restore registers.  We can use ebp or esp to address the memory
5092          locations.  If both are available, default to ebp, since offsets
5093          are known to be small.  Only exception is esp pointing directly to the
5094          end of block of saved registers, where we may simplify addressing
5095          mode.  */
5096
5097       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
5098         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
5099                                           frame.to_allocate, style == 2);
5100       else
5101         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
5102                                           offset, style == 2);
5103
5104       /* eh_return epilogues need %ecx added to the stack pointer.  */
5105       if (style == 2)
5106         {
5107           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
5108
5109           if (frame_pointer_needed)
5110             {
5111               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
5112               tmp = plus_constant (tmp, UNITS_PER_WORD);
5113               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
5114
5115               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
5116               emit_move_insn (hard_frame_pointer_rtx, tmp);
5117
5118               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
5119                                          const0_rtx, style);
5120             }
5121           else
5122             {
5123               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5124               tmp = plus_constant (tmp, (frame.to_allocate
5125                                          + frame.nregs * UNITS_PER_WORD));
5126               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5127             }
5128         }
5129       else if (!frame_pointer_needed)
5130         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5131                                    GEN_INT (frame.to_allocate
5132                                             + frame.nregs * UNITS_PER_WORD),
5133                                    style);
5134       /* If not an i386, mov & pop is faster than "leave".  */
5135       else if (TARGET_USE_LEAVE || optimize_size
5136                || !cfun->machine->use_fast_prologue_epilogue)
5137         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5138       else
5139         {
5140           pro_epilogue_adjust_stack (stack_pointer_rtx,
5141                                      hard_frame_pointer_rtx,
5142                                      const0_rtx, style);
5143           if (TARGET_64BIT)
5144             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5145           else
5146             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5147         }
5148     }
5149   else
5150     {
5151       /* First step is to deallocate the stack frame so that we can
5152          pop the registers.  */
5153       if (!sp_valid)
5154         {
5155           gcc_assert (frame_pointer_needed);
5156           pro_epilogue_adjust_stack (stack_pointer_rtx,
5157                                      hard_frame_pointer_rtx,
5158                                      GEN_INT (offset), style);
5159         }
5160       else if (frame.to_allocate)
5161         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5162                                    GEN_INT (frame.to_allocate), style);
5163
5164       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5165         if (ix86_save_reg (regno, false))
5166           {
5167             if (TARGET_64BIT)
5168               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5169             else
5170               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5171           }
5172       if (frame_pointer_needed)
5173         {
5174           /* Leave results in shorter dependency chains on CPUs that are
5175              able to grok it fast.  */
5176           if (TARGET_USE_LEAVE)
5177             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5178           else if (TARGET_64BIT)
5179             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5180           else
5181             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5182         }
5183     }
5184
5185   if (cfun->machine->force_align_arg_pointer)
5186     {
5187       emit_insn (gen_addsi3 (stack_pointer_rtx,
5188                              cfun->machine->force_align_arg_pointer,
5189                              GEN_INT (-4)));
5190     }
5191
5192   /* Sibcall epilogues don't want a return instruction.  */
5193   if (style == 0)
5194     return;
5195
5196   if (current_function_pops_args && current_function_args_size)
5197     {
5198       rtx popc = GEN_INT (current_function_pops_args);
5199
5200       /* i386 can only pop 64K bytes.  If asked to pop more, pop
5201          return address, do explicit add, and jump indirectly to the
5202          caller.  */
5203
5204       if (current_function_pops_args >= 65536)
5205         {
5206           rtx ecx = gen_rtx_REG (SImode, 2);
5207
5208           /* There is no "pascal" calling convention in 64bit ABI.  */
5209           gcc_assert (!TARGET_64BIT);
5210
5211           emit_insn (gen_popsi1 (ecx));
5212           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
5213           emit_jump_insn (gen_return_indirect_internal (ecx));
5214         }
5215       else
5216         emit_jump_insn (gen_return_pop_internal (popc));
5217     }
5218   else
5219     emit_jump_insn (gen_return_internal ());
5220 }
5221
5222 /* Reset from the function's potential modifications.  */
5223
5224 static void
5225 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5226                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
5227 {
5228   if (pic_offset_table_rtx)
5229     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
5230 }
5231 \f
5232 /* Extract the parts of an RTL expression that is a valid memory address
5233    for an instruction.  Return 0 if the structure of the address is
5234    grossly off.  Return -1 if the address contains ASHIFT, so it is not
5235    strictly valid, but still used for computing length of lea instruction.  */
5236
5237 int
5238 ix86_decompose_address (rtx addr, struct ix86_address *out)
5239 {
5240   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
5241   rtx base_reg, index_reg;
5242   HOST_WIDE_INT scale = 1;
5243   rtx scale_rtx = NULL_RTX;
5244   int retval = 1;
5245   enum ix86_address_seg seg = SEG_DEFAULT;
5246
5247   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
5248     base = addr;
5249   else if (GET_CODE (addr) == PLUS)
5250     {
5251       rtx addends[4], op;
5252       int n = 0, i;
5253
5254       op = addr;
5255       do
5256         {
5257           if (n >= 4)
5258             return 0;
5259           addends[n++] = XEXP (op, 1);
5260           op = XEXP (op, 0);
5261         }
5262       while (GET_CODE (op) == PLUS);
5263       if (n >= 4)
5264         return 0;
5265       addends[n] = op;
5266
5267       for (i = n; i >= 0; --i)
5268         {
5269           op = addends[i];
5270           switch (GET_CODE (op))
5271             {
5272             case MULT:
5273               if (index)
5274                 return 0;
5275               index = XEXP (op, 0);
5276               scale_rtx = XEXP (op, 1);
5277               break;
5278
5279             case UNSPEC:
5280               if (XINT (op, 1) == UNSPEC_TP
5281                   && TARGET_TLS_DIRECT_SEG_REFS
5282                   && seg == SEG_DEFAULT)
5283                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
5284               else
5285                 return 0;
5286               break;
5287
5288             case REG:
5289             case SUBREG:
5290               if (!base)
5291                 base = op;
5292               else if (!index)
5293                 index = op;
5294               else
5295                 return 0;
5296               break;
5297
5298             case CONST:
5299             case CONST_INT:
5300             case SYMBOL_REF:
5301             case LABEL_REF:
5302               if (disp)
5303                 return 0;
5304               disp = op;
5305               break;
5306
5307             default:
5308               return 0;
5309             }
5310         }
5311     }
5312   else if (GET_CODE (addr) == MULT)
5313     {
5314       index = XEXP (addr, 0);           /* index*scale */
5315       scale_rtx = XEXP (addr, 1);
5316     }
5317   else if (GET_CODE (addr) == ASHIFT)
5318     {
5319       rtx tmp;
5320
5321       /* We're called for lea too, which implements ashift on occasion.  */
5322       index = XEXP (addr, 0);
5323       tmp = XEXP (addr, 1);
5324       if (GET_CODE (tmp) != CONST_INT)
5325         return 0;
5326       scale = INTVAL (tmp);
5327       if ((unsigned HOST_WIDE_INT) scale > 3)
5328         return 0;
5329       scale = 1 << scale;
5330       retval = -1;
5331     }
5332   else
5333     disp = addr;                        /* displacement */
5334
5335   /* Extract the integral value of scale.  */
5336   if (scale_rtx)
5337     {
5338       if (GET_CODE (scale_rtx) != CONST_INT)
5339         return 0;
5340       scale = INTVAL (scale_rtx);
5341     }
5342
5343   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
5344   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
5345
5346   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
5347   if (base_reg && index_reg && scale == 1
5348       && (index_reg == arg_pointer_rtx
5349           || index_reg == frame_pointer_rtx
5350           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
5351     {
5352       rtx tmp;
5353       tmp = base, base = index, index = tmp;
5354       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
5355     }
5356
5357   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
5358   if ((base_reg == hard_frame_pointer_rtx
5359        || base_reg == frame_pointer_rtx
5360        || base_reg == arg_pointer_rtx) && !disp)
5361     disp = const0_rtx;
5362
5363   /* Special case: on K6, [%esi] makes the instruction vector decoded.
5364      Avoid this by transforming to [%esi+0].  */
5365   if (ix86_tune == PROCESSOR_K6 && !optimize_size
5366       && base_reg && !index_reg && !disp
5367       && REG_P (base_reg)
5368       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
5369     disp = const0_rtx;
5370
5371   /* Special case: encode reg+reg instead of reg*2.  */
5372   if (!base && index && scale && scale == 2)
5373     base = index, base_reg = index_reg, scale = 1;
5374
5375   /* Special case: scaling cannot be encoded without base or displacement.  */
5376   if (!base && !disp && index && scale != 1)
5377     disp = const0_rtx;
5378
5379   out->base = base;
5380   out->index = index;
5381   out->disp = disp;
5382   out->scale = scale;
5383   out->seg = seg;
5384
5385   return retval;
5386 }
5387 \f
5388 /* Return cost of the memory address x.
5389    For i386, it is better to use a complex address than let gcc copy
5390    the address into a reg and make a new pseudo.  But not if the address
5391    requires to two regs - that would mean more pseudos with longer
5392    lifetimes.  */
5393 static int
5394 ix86_address_cost (rtx x)
5395 {
5396   struct ix86_address parts;
5397   int cost = 1;
5398   int ok = ix86_decompose_address (x, &parts);
5399
5400   gcc_assert (ok);
5401
5402   if (parts.base && GET_CODE (parts.base) == SUBREG)
5403     parts.base = SUBREG_REG (parts.base);
5404   if (parts.index && GET_CODE (parts.index) == SUBREG)
5405     parts.index = SUBREG_REG (parts.index);
5406
5407   /* More complex memory references are better.  */
5408   if (parts.disp && parts.disp != const0_rtx)
5409     cost--;
5410   if (parts.seg != SEG_DEFAULT)
5411     cost--;
5412
5413   /* Attempt to minimize number of registers in the address.  */
5414   if ((parts.base
5415        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
5416       || (parts.index
5417           && (!REG_P (parts.index)
5418               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
5419     cost++;
5420
5421   if (parts.base
5422       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
5423       && parts.index
5424       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
5425       && parts.base != parts.index)
5426     cost++;
5427
5428   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
5429      since it's predecode logic can't detect the length of instructions
5430      and it degenerates to vector decoded.  Increase cost of such
5431      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
5432      to split such addresses or even refuse such addresses at all.
5433
5434      Following addressing modes are affected:
5435       [base+scale*index]
5436       [scale*index+disp]
5437       [base+index]
5438
5439      The first and last case  may be avoidable by explicitly coding the zero in
5440      memory address, but I don't have AMD-K6 machine handy to check this
5441      theory.  */
5442
5443   if (TARGET_K6
5444       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
5445           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
5446           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
5447     cost += 10;
5448
5449   return cost;
5450 }
5451 \f
5452 /* If X is a machine specific address (i.e. a symbol or label being
5453    referenced as a displacement from the GOT implemented using an
5454    UNSPEC), then return the base term.  Otherwise return X.  */
5455
5456 rtx
5457 ix86_find_base_term (rtx x)
5458 {
5459   rtx term;
5460
5461   if (TARGET_64BIT)
5462     {
5463       if (GET_CODE (x) != CONST)
5464         return x;
5465       term = XEXP (x, 0);
5466       if (GET_CODE (term) == PLUS
5467           && (GET_CODE (XEXP (term, 1)) == CONST_INT
5468               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
5469         term = XEXP (term, 0);
5470       if (GET_CODE (term) != UNSPEC
5471           || XINT (term, 1) != UNSPEC_GOTPCREL)
5472         return x;
5473
5474       term = XVECEXP (term, 0, 0);
5475
5476       if (GET_CODE (term) != SYMBOL_REF
5477           && GET_CODE (term) != LABEL_REF)
5478         return x;
5479
5480       return term;
5481     }
5482
5483   term = ix86_delegitimize_address (x);
5484
5485   if (GET_CODE (term) != SYMBOL_REF
5486       && GET_CODE (term) != LABEL_REF)
5487     return x;
5488
5489   return term;
5490 }
5491
5492 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
5493    this is used for to form addresses to local data when -fPIC is in
5494    use.  */
5495
5496 static bool
5497 darwin_local_data_pic (rtx disp)
5498 {
5499   if (GET_CODE (disp) == MINUS)
5500     {
5501       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
5502           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
5503         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
5504           {
5505             const char *sym_name = XSTR (XEXP (disp, 1), 0);
5506             if (! strcmp (sym_name, "<pic base>"))
5507               return true;
5508           }
5509     }
5510
5511   return false;
5512 }
5513 \f
5514 /* Determine if a given RTX is a valid constant.  We already know this
5515    satisfies CONSTANT_P.  */
5516
5517 bool
5518 legitimate_constant_p (rtx x)
5519 {
5520   switch (GET_CODE (x))
5521     {
5522     case CONST:
5523       x = XEXP (x, 0);
5524
5525       if (GET_CODE (x) == PLUS)
5526         {
5527           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5528             return false;
5529           x = XEXP (x, 0);
5530         }
5531
5532       if (TARGET_MACHO && darwin_local_data_pic (x))
5533         return true;
5534
5535       /* Only some unspecs are valid as "constants".  */
5536       if (GET_CODE (x) == UNSPEC)
5537         switch (XINT (x, 1))
5538           {
5539           case UNSPEC_GOTOFF:
5540             return TARGET_64BIT;
5541           case UNSPEC_TPOFF:
5542           case UNSPEC_NTPOFF:
5543             x = XVECEXP (x, 0, 0);
5544             return (GET_CODE (x) == SYMBOL_REF
5545                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
5546           case UNSPEC_DTPOFF:
5547             x = XVECEXP (x, 0, 0);
5548             return (GET_CODE (x) == SYMBOL_REF
5549                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
5550           default:
5551             return false;
5552           }
5553
5554       /* We must have drilled down to a symbol.  */
5555       if (GET_CODE (x) == LABEL_REF)
5556         return true;
5557       if (GET_CODE (x) != SYMBOL_REF)
5558         return false;
5559       /* FALLTHRU */
5560
5561     case SYMBOL_REF:
5562       /* TLS symbols are never valid.  */
5563       if (SYMBOL_REF_TLS_MODEL (x))
5564         return false;
5565       break;
5566
5567     default:
5568       break;
5569     }
5570
5571   /* Otherwise we handle everything else in the move patterns.  */
5572   return true;
5573 }
5574
5575 /* Determine if it's legal to put X into the constant pool.  This
5576    is not possible for the address of thread-local symbols, which
5577    is checked above.  */
5578
5579 static bool
5580 ix86_cannot_force_const_mem (rtx x)
5581 {
5582   return !legitimate_constant_p (x);
5583 }
5584
5585 /* Determine if a given RTX is a valid constant address.  */
5586
5587 bool
5588 constant_address_p (rtx x)
5589 {
5590   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
5591 }
5592
5593 /* Nonzero if the constant value X is a legitimate general operand
5594    when generating PIC code.  It is given that flag_pic is on and
5595    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
5596
5597 bool
5598 legitimate_pic_operand_p (rtx x)
5599 {
5600   rtx inner;
5601
5602   switch (GET_CODE (x))
5603     {
5604     case CONST:
5605       inner = XEXP (x, 0);
5606       if (GET_CODE (inner) == PLUS
5607           && GET_CODE (XEXP (inner, 1)) == CONST_INT)
5608         inner = XEXP (inner, 0);
5609
5610       /* Only some unspecs are valid as "constants".  */
5611       if (GET_CODE (inner) == UNSPEC)
5612         switch (XINT (inner, 1))
5613           {
5614           case UNSPEC_GOTOFF:
5615             return TARGET_64BIT;
5616           case UNSPEC_TPOFF:
5617             x = XVECEXP (inner, 0, 0);
5618             return (GET_CODE (x) == SYMBOL_REF
5619                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
5620           default:
5621             return false;
5622           }
5623       /* FALLTHRU */
5624
5625     case SYMBOL_REF:
5626     case LABEL_REF:
5627       return legitimate_pic_address_disp_p (x);
5628
5629     default:
5630       return true;
5631     }
5632 }
5633
5634 /* Determine if a given CONST RTX is a valid memory displacement
5635    in PIC mode.  */
5636
5637 int
5638 legitimate_pic_address_disp_p (rtx disp)
5639 {
5640   bool saw_plus;
5641
5642   /* In 64bit mode we can allow direct addresses of symbols and labels
5643      when they are not dynamic symbols.  */
5644   if (TARGET_64BIT)
5645     {
5646       rtx op0 = disp, op1;
5647
5648       switch (GET_CODE (disp))
5649         {
5650         case LABEL_REF:
5651           return true;
5652
5653         case CONST:
5654           if (GET_CODE (XEXP (disp, 0)) != PLUS)
5655             break;
5656           op0 = XEXP (XEXP (disp, 0), 0);
5657           op1 = XEXP (XEXP (disp, 0), 1);
5658           if (GET_CODE (op1) != CONST_INT
5659               || INTVAL (op1) >= 16*1024*1024
5660               || INTVAL (op1) < -16*1024*1024)
5661             break;
5662           if (GET_CODE (op0) == LABEL_REF)
5663             return true;
5664           if (GET_CODE (op0) != SYMBOL_REF)
5665             break;
5666           /* FALLTHRU */
5667
5668         case SYMBOL_REF:
5669           /* TLS references should always be enclosed in UNSPEC.  */
5670           if (SYMBOL_REF_TLS_MODEL (op0))
5671             return false;
5672           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0))
5673             return true;
5674           break;
5675
5676         default:
5677           break;
5678         }
5679     }
5680   if (GET_CODE (disp) != CONST)
5681     return 0;
5682   disp = XEXP (disp, 0);
5683
5684   if (TARGET_64BIT)
5685     {
5686       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
5687          of GOT tables.  We should not need these anyway.  */
5688       if (GET_CODE (disp) != UNSPEC
5689           || (XINT (disp, 1) != UNSPEC_GOTPCREL
5690               && XINT (disp, 1) != UNSPEC_GOTOFF))
5691         return 0;
5692
5693       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
5694           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
5695         return 0;
5696       return 1;
5697     }
5698
5699   saw_plus = false;
5700   if (GET_CODE (disp) == PLUS)
5701     {
5702       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
5703         return 0;
5704       disp = XEXP (disp, 0);
5705       saw_plus = true;
5706     }
5707
5708   if (TARGET_MACHO && darwin_local_data_pic (disp))
5709     return 1;
5710
5711   if (GET_CODE (disp) != UNSPEC)
5712     return 0;
5713
5714   switch (XINT (disp, 1))
5715     {
5716     case UNSPEC_GOT:
5717       if (saw_plus)
5718         return false;
5719       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
5720     case UNSPEC_GOTOFF:
5721       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
5722          While ABI specify also 32bit relocation but we don't produce it in
5723          small PIC model at all.  */
5724       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
5725            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
5726           && !TARGET_64BIT)
5727         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5728       return false;
5729     case UNSPEC_GOTTPOFF:
5730     case UNSPEC_GOTNTPOFF:
5731     case UNSPEC_INDNTPOFF:
5732       if (saw_plus)
5733         return false;
5734       disp = XVECEXP (disp, 0, 0);
5735       return (GET_CODE (disp) == SYMBOL_REF
5736               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
5737     case UNSPEC_NTPOFF:
5738       disp = XVECEXP (disp, 0, 0);
5739       return (GET_CODE (disp) == SYMBOL_REF
5740               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
5741     case UNSPEC_DTPOFF:
5742       disp = XVECEXP (disp, 0, 0);
5743       return (GET_CODE (disp) == SYMBOL_REF
5744               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
5745     }
5746
5747   return 0;
5748 }
5749
5750 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
5751    memory address for an instruction.  The MODE argument is the machine mode
5752    for the MEM expression that wants to use this address.
5753
5754    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
5755    convert common non-canonical forms to canonical form so that they will
5756    be recognized.  */
5757
5758 int
5759 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
5760 {
5761   struct ix86_address parts;
5762   rtx base, index, disp;
5763   HOST_WIDE_INT scale;
5764   const char *reason = NULL;
5765   rtx reason_rtx = NULL_RTX;
5766
5767   if (TARGET_DEBUG_ADDR)
5768     {
5769       fprintf (stderr,
5770                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
5771                GET_MODE_NAME (mode), strict);
5772       debug_rtx (addr);
5773     }
5774
5775   if (ix86_decompose_address (addr, &parts) <= 0)
5776     {
5777       reason = "decomposition failed";
5778       goto report_error;
5779     }
5780
5781   base = parts.base;
5782   index = parts.index;
5783   disp = parts.disp;
5784   scale = parts.scale;
5785
5786   /* Validate base register.
5787
5788      Don't allow SUBREG's that span more than a word here.  It can lead to spill
5789      failures when the base is one word out of a two word structure, which is
5790      represented internally as a DImode int.  */
5791
5792   if (base)
5793     {
5794       rtx reg;
5795       reason_rtx = base;
5796   
5797       if (REG_P (base))
5798         reg = base;
5799       else if (GET_CODE (base) == SUBREG
5800                && REG_P (SUBREG_REG (base))
5801                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
5802                   <= UNITS_PER_WORD)
5803         reg = SUBREG_REG (base);
5804       else
5805         {
5806           reason = "base is not a register";
5807           goto report_error;
5808         }
5809
5810       if (GET_MODE (base) != Pmode)
5811         {
5812           reason = "base is not in Pmode";
5813           goto report_error;
5814         }
5815
5816       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
5817           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
5818         {
5819           reason = "base is not valid";
5820           goto report_error;
5821         }
5822     }
5823
5824   /* Validate index register.
5825
5826      Don't allow SUBREG's that span more than a word here -- same as above.  */
5827
5828   if (index)
5829     {
5830       rtx reg;
5831       reason_rtx = index;
5832
5833       if (REG_P (index))
5834         reg = index;
5835       else if (GET_CODE (index) == SUBREG
5836                && REG_P (SUBREG_REG (index))
5837                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
5838                   <= UNITS_PER_WORD)
5839         reg = SUBREG_REG (index);
5840       else
5841         {
5842           reason = "index is not a register";
5843           goto report_error;
5844         }
5845
5846       if (GET_MODE (index) != Pmode)
5847         {
5848           reason = "index is not in Pmode";
5849           goto report_error;
5850         }
5851
5852       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
5853           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
5854         {
5855           reason = "index is not valid";
5856           goto report_error;
5857         }
5858     }
5859
5860   /* Validate scale factor.  */
5861   if (scale != 1)
5862     {
5863       reason_rtx = GEN_INT (scale);
5864       if (!index)
5865         {
5866           reason = "scale without index";
5867           goto report_error;
5868         }
5869
5870       if (scale != 2 && scale != 4 && scale != 8)
5871         {
5872           reason = "scale is not a valid multiplier";
5873           goto report_error;
5874         }
5875     }
5876
5877   /* Validate displacement.  */
5878   if (disp)
5879     {
5880       reason_rtx = disp;
5881
5882       if (GET_CODE (disp) == CONST
5883           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
5884         switch (XINT (XEXP (disp, 0), 1))
5885           {
5886           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
5887              used.  While ABI specify also 32bit relocations, we don't produce
5888              them at all and use IP relative instead.  */
5889           case UNSPEC_GOT:
5890           case UNSPEC_GOTOFF:
5891             gcc_assert (flag_pic);
5892             if (!TARGET_64BIT)
5893               goto is_legitimate_pic;
5894             reason = "64bit address unspec";
5895             goto report_error;
5896  
5897           case UNSPEC_GOTPCREL:
5898             gcc_assert (flag_pic);
5899             goto is_legitimate_pic;
5900
5901           case UNSPEC_GOTTPOFF:
5902           case UNSPEC_GOTNTPOFF:
5903           case UNSPEC_INDNTPOFF:
5904           case UNSPEC_NTPOFF:
5905           case UNSPEC_DTPOFF:
5906             break;
5907
5908           default:
5909             reason = "invalid address unspec";
5910             goto report_error;
5911           }
5912
5913       else if (flag_pic && (SYMBOLIC_CONST (disp)
5914 #if TARGET_MACHO
5915                             && !machopic_operand_p (disp)
5916 #endif
5917                             ))
5918         {
5919         is_legitimate_pic:
5920           if (TARGET_64BIT && (index || base))
5921             {
5922               /* foo@dtpoff(%rX) is ok.  */
5923               if (GET_CODE (disp) != CONST
5924                   || GET_CODE (XEXP (disp, 0)) != PLUS
5925                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
5926                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
5927                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
5928                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
5929                 {
5930                   reason = "non-constant pic memory reference";
5931                   goto report_error;
5932                 }
5933             }
5934           else if (! legitimate_pic_address_disp_p (disp))
5935             {
5936               reason = "displacement is an invalid pic construct";
5937               goto report_error;
5938             }
5939
5940           /* This code used to verify that a symbolic pic displacement
5941              includes the pic_offset_table_rtx register.
5942
5943              While this is good idea, unfortunately these constructs may
5944              be created by "adds using lea" optimization for incorrect
5945              code like:
5946
5947              int a;
5948              int foo(int i)
5949                {
5950                  return *(&a+i);
5951                }
5952
5953              This code is nonsensical, but results in addressing
5954              GOT table with pic_offset_table_rtx base.  We can't
5955              just refuse it easily, since it gets matched by
5956              "addsi3" pattern, that later gets split to lea in the
5957              case output register differs from input.  While this
5958              can be handled by separate addsi pattern for this case
5959              that never results in lea, this seems to be easier and
5960              correct fix for crash to disable this test.  */
5961         }
5962       else if (GET_CODE (disp) != LABEL_REF
5963                && GET_CODE (disp) != CONST_INT
5964                && (GET_CODE (disp) != CONST
5965                    || !legitimate_constant_p (disp))
5966                && (GET_CODE (disp) != SYMBOL_REF
5967                    || !legitimate_constant_p (disp)))
5968         {
5969           reason = "displacement is not constant";
5970           goto report_error;
5971         }
5972       else if (TARGET_64BIT
5973                && !x86_64_immediate_operand (disp, VOIDmode))
5974         {
5975           reason = "displacement is out of range";
5976           goto report_error;
5977         }
5978     }
5979
5980   /* Everything looks valid.  */
5981   if (TARGET_DEBUG_ADDR)
5982     fprintf (stderr, "Success.\n");
5983   return TRUE;
5984
5985  report_error:
5986   if (TARGET_DEBUG_ADDR)
5987     {
5988       fprintf (stderr, "Error: %s\n", reason);
5989       debug_rtx (reason_rtx);
5990     }
5991   return FALSE;
5992 }
5993 \f
5994 /* Return a unique alias set for the GOT.  */
5995
5996 static HOST_WIDE_INT
5997 ix86_GOT_alias_set (void)
5998 {
5999   static HOST_WIDE_INT set = -1;
6000   if (set == -1)
6001     set = new_alias_set ();
6002   return set;
6003 }
6004
6005 /* Return a legitimate reference for ORIG (an address) using the
6006    register REG.  If REG is 0, a new pseudo is generated.
6007
6008    There are two types of references that must be handled:
6009
6010    1. Global data references must load the address from the GOT, via
6011       the PIC reg.  An insn is emitted to do this load, and the reg is
6012       returned.
6013
6014    2. Static data references, constant pool addresses, and code labels
6015       compute the address as an offset from the GOT, whose base is in
6016       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6017       differentiate them from global data objects.  The returned
6018       address is the PIC reg + an unspec constant.
6019
6020    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6021    reg also appears in the address.  */
6022
6023 static rtx
6024 legitimize_pic_address (rtx orig, rtx reg)
6025 {
6026   rtx addr = orig;
6027   rtx new = orig;
6028   rtx base;
6029
6030 #if TARGET_MACHO
6031   if (reg == 0)
6032     reg = gen_reg_rtx (Pmode);
6033   /* Use the generic Mach-O PIC machinery.  */
6034   return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6035 #endif
6036
6037   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
6038     new = addr;
6039   else if (TARGET_64BIT
6040            && ix86_cmodel != CM_SMALL_PIC
6041            && local_symbolic_operand (addr, Pmode))
6042     {
6043       rtx tmpreg;
6044       /* This symbol may be referenced via a displacement from the PIC
6045          base address (@GOTOFF).  */
6046
6047       if (reload_in_progress)
6048         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6049       if (GET_CODE (addr) == CONST)
6050         addr = XEXP (addr, 0);
6051       if (GET_CODE (addr) == PLUS)
6052           {
6053             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6054             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6055           }
6056         else
6057           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6058       new = gen_rtx_CONST (Pmode, new);
6059       if (!reg)
6060         tmpreg = gen_reg_rtx (Pmode);
6061       else
6062         tmpreg = reg;
6063       emit_move_insn (tmpreg, new);
6064
6065       if (reg != 0)
6066         {
6067           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
6068                                      tmpreg, 1, OPTAB_DIRECT);
6069           new = reg;
6070         }
6071       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
6072     }
6073   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
6074     {
6075       /* This symbol may be referenced via a displacement from the PIC
6076          base address (@GOTOFF).  */
6077
6078       if (reload_in_progress)
6079         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6080       if (GET_CODE (addr) == CONST)
6081         addr = XEXP (addr, 0);
6082       if (GET_CODE (addr) == PLUS)
6083           {
6084             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6085             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6086           }
6087         else
6088           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6089       new = gen_rtx_CONST (Pmode, new);
6090       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6091
6092       if (reg != 0)
6093         {
6094           emit_move_insn (reg, new);
6095           new = reg;
6096         }
6097     }
6098   else if (GET_CODE (addr) == SYMBOL_REF)
6099     {
6100       if (TARGET_64BIT)
6101         {
6102           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
6103           new = gen_rtx_CONST (Pmode, new);
6104           new = gen_const_mem (Pmode, new);
6105           set_mem_alias_set (new, ix86_GOT_alias_set ());
6106
6107           if (reg == 0)
6108             reg = gen_reg_rtx (Pmode);
6109           /* Use directly gen_movsi, otherwise the address is loaded
6110              into register for CSE.  We don't want to CSE this addresses,
6111              instead we CSE addresses from the GOT table, so skip this.  */
6112           emit_insn (gen_movsi (reg, new));
6113           new = reg;
6114         }
6115       else
6116         {
6117           /* This symbol must be referenced via a load from the
6118              Global Offset Table (@GOT).  */
6119
6120           if (reload_in_progress)
6121             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6122           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
6123           new = gen_rtx_CONST (Pmode, new);
6124           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6125           new = gen_const_mem (Pmode, new);
6126           set_mem_alias_set (new, ix86_GOT_alias_set ());
6127
6128           if (reg == 0)
6129             reg = gen_reg_rtx (Pmode);
6130           emit_move_insn (reg, new);
6131           new = reg;
6132         }
6133     }
6134   else
6135     {
6136       if (GET_CODE (addr) == CONST_INT
6137           && !x86_64_immediate_operand (addr, VOIDmode))
6138         {
6139           if (reg)
6140             {
6141               emit_move_insn (reg, addr);
6142               new = reg;
6143             }
6144           else
6145             new = force_reg (Pmode, addr);
6146         }
6147       else if (GET_CODE (addr) == CONST)
6148         {
6149           addr = XEXP (addr, 0);
6150
6151           /* We must match stuff we generate before.  Assume the only
6152              unspecs that can get here are ours.  Not that we could do
6153              anything with them anyway....  */
6154           if (GET_CODE (addr) == UNSPEC
6155               || (GET_CODE (addr) == PLUS
6156                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
6157             return orig;
6158           gcc_assert (GET_CODE (addr) == PLUS);
6159         }
6160       if (GET_CODE (addr) == PLUS)
6161         {
6162           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
6163
6164           /* Check first to see if this is a constant offset from a @GOTOFF
6165              symbol reference.  */
6166           if (local_symbolic_operand (op0, Pmode)
6167               && GET_CODE (op1) == CONST_INT)
6168             {
6169               if (!TARGET_64BIT)
6170                 {
6171                   if (reload_in_progress)
6172                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6173                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
6174                                         UNSPEC_GOTOFF);
6175                   new = gen_rtx_PLUS (Pmode, new, op1);
6176                   new = gen_rtx_CONST (Pmode, new);
6177                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6178
6179                   if (reg != 0)
6180                     {
6181                       emit_move_insn (reg, new);
6182                       new = reg;
6183                     }
6184                 }
6185               else
6186                 {
6187                   if (INTVAL (op1) < -16*1024*1024
6188                       || INTVAL (op1) >= 16*1024*1024)
6189                     {
6190                       if (!x86_64_immediate_operand (op1, Pmode))
6191                         op1 = force_reg (Pmode, op1);
6192                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
6193                     }
6194                 }
6195             }
6196           else
6197             {
6198               base = legitimize_pic_address (XEXP (addr, 0), reg);
6199               new  = legitimize_pic_address (XEXP (addr, 1),
6200                                              base == reg ? NULL_RTX : reg);
6201
6202               if (GET_CODE (new) == CONST_INT)
6203                 new = plus_constant (base, INTVAL (new));
6204               else
6205                 {
6206                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
6207                     {
6208                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
6209                       new = XEXP (new, 1);
6210                     }
6211                   new = gen_rtx_PLUS (Pmode, base, new);
6212                 }
6213             }
6214         }
6215     }
6216   return new;
6217 }
6218 \f
6219 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
6220
6221 static rtx
6222 get_thread_pointer (int to_reg)
6223 {
6224   rtx tp, reg, insn;
6225
6226   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
6227   if (!to_reg)
6228     return tp;
6229
6230   reg = gen_reg_rtx (Pmode);
6231   insn = gen_rtx_SET (VOIDmode, reg, tp);
6232   insn = emit_insn (insn);
6233
6234   return reg;
6235 }
6236
6237 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
6238    false if we expect this to be used for a memory address and true if
6239    we expect to load the address into a register.  */
6240
6241 static rtx
6242 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
6243 {
6244   rtx dest, base, off, pic;
6245   int type;
6246
6247   switch (model)
6248     {
6249     case TLS_MODEL_GLOBAL_DYNAMIC:
6250       dest = gen_reg_rtx (Pmode);
6251       if (TARGET_64BIT)
6252         {
6253           rtx rax = gen_rtx_REG (Pmode, 0), insns;
6254
6255           start_sequence ();
6256           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
6257           insns = get_insns ();
6258           end_sequence ();
6259
6260           emit_libcall_block (insns, dest, rax, x);
6261         }
6262       else
6263         emit_insn (gen_tls_global_dynamic_32 (dest, x));
6264       break;
6265
6266     case TLS_MODEL_LOCAL_DYNAMIC:
6267       base = gen_reg_rtx (Pmode);
6268       if (TARGET_64BIT)
6269         {
6270           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
6271
6272           start_sequence ();
6273           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
6274           insns = get_insns ();
6275           end_sequence ();
6276
6277           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
6278           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
6279           emit_libcall_block (insns, base, rax, note);
6280         }
6281       else
6282         emit_insn (gen_tls_local_dynamic_base_32 (base));
6283
6284       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
6285       off = gen_rtx_CONST (Pmode, off);
6286
6287       return gen_rtx_PLUS (Pmode, base, off);
6288
6289     case TLS_MODEL_INITIAL_EXEC:
6290       if (TARGET_64BIT)
6291         {
6292           pic = NULL;
6293           type = UNSPEC_GOTNTPOFF;
6294         }
6295       else if (flag_pic)
6296         {
6297           if (reload_in_progress)
6298             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6299           pic = pic_offset_table_rtx;
6300           type = TARGET_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
6301         }
6302       else if (!TARGET_GNU_TLS)
6303         {
6304           pic = gen_reg_rtx (Pmode);
6305           emit_insn (gen_set_got (pic));
6306           type = UNSPEC_GOTTPOFF;
6307         }
6308       else
6309         {
6310           pic = NULL;
6311           type = UNSPEC_INDNTPOFF;
6312         }
6313
6314       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
6315       off = gen_rtx_CONST (Pmode, off);
6316       if (pic)
6317         off = gen_rtx_PLUS (Pmode, pic, off);
6318       off = gen_const_mem (Pmode, off);
6319       set_mem_alias_set (off, ix86_GOT_alias_set ());
6320
6321       if (TARGET_64BIT || TARGET_GNU_TLS)
6322         {
6323           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6324           off = force_reg (Pmode, off);
6325           return gen_rtx_PLUS (Pmode, base, off);
6326         }
6327       else
6328         {
6329           base = get_thread_pointer (true);
6330           dest = gen_reg_rtx (Pmode);
6331           emit_insn (gen_subsi3 (dest, base, off));
6332         }
6333       break;
6334
6335     case TLS_MODEL_LOCAL_EXEC:
6336       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
6337                             (TARGET_64BIT || TARGET_GNU_TLS)
6338                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
6339       off = gen_rtx_CONST (Pmode, off);
6340
6341       if (TARGET_64BIT || TARGET_GNU_TLS)
6342         {
6343           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6344           return gen_rtx_PLUS (Pmode, base, off);
6345         }
6346       else
6347         {
6348           base = get_thread_pointer (true);
6349           dest = gen_reg_rtx (Pmode);
6350           emit_insn (gen_subsi3 (dest, base, off));
6351         }
6352       break;
6353
6354     default:
6355       gcc_unreachable ();
6356     }
6357
6358   return dest;
6359 }
6360
6361 /* Try machine-dependent ways of modifying an illegitimate address
6362    to be legitimate.  If we find one, return the new, valid address.
6363    This macro is used in only one place: `memory_address' in explow.c.
6364
6365    OLDX is the address as it was before break_out_memory_refs was called.
6366    In some cases it is useful to look at this to decide what needs to be done.
6367
6368    MODE and WIN are passed so that this macro can use
6369    GO_IF_LEGITIMATE_ADDRESS.
6370
6371    It is always safe for this macro to do nothing.  It exists to recognize
6372    opportunities to optimize the output.
6373
6374    For the 80386, we handle X+REG by loading X into a register R and
6375    using R+REG.  R will go in a general reg and indexing will be used.
6376    However, if REG is a broken-out memory address or multiplication,
6377    nothing needs to be done because REG can certainly go in a general reg.
6378
6379    When -fpic is used, special handling is needed for symbolic references.
6380    See comments by legitimize_pic_address in i386.c for details.  */
6381
6382 rtx
6383 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
6384 {
6385   int changed = 0;
6386   unsigned log;
6387
6388   if (TARGET_DEBUG_ADDR)
6389     {
6390       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
6391                GET_MODE_NAME (mode));
6392       debug_rtx (x);
6393     }
6394
6395   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
6396   if (log)
6397     return legitimize_tls_address (x, log, false);
6398   if (GET_CODE (x) == CONST
6399       && GET_CODE (XEXP (x, 0)) == PLUS
6400       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6401       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
6402     {
6403       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
6404       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
6405     }
6406
6407   if (flag_pic && SYMBOLIC_CONST (x))
6408     return legitimize_pic_address (x, 0);
6409
6410   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
6411   if (GET_CODE (x) == ASHIFT
6412       && GET_CODE (XEXP (x, 1)) == CONST_INT
6413       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
6414     {
6415       changed = 1;
6416       log = INTVAL (XEXP (x, 1));
6417       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
6418                         GEN_INT (1 << log));
6419     }
6420
6421   if (GET_CODE (x) == PLUS)
6422     {
6423       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
6424
6425       if (GET_CODE (XEXP (x, 0)) == ASHIFT
6426           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6427           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
6428         {
6429           changed = 1;
6430           log = INTVAL (XEXP (XEXP (x, 0), 1));
6431           XEXP (x, 0) = gen_rtx_MULT (Pmode,
6432                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
6433                                       GEN_INT (1 << log));
6434         }
6435
6436       if (GET_CODE (XEXP (x, 1)) == ASHIFT
6437           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
6438           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
6439         {
6440           changed = 1;
6441           log = INTVAL (XEXP (XEXP (x, 1), 1));
6442           XEXP (x, 1) = gen_rtx_MULT (Pmode,
6443                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
6444                                       GEN_INT (1 << log));
6445         }
6446
6447       /* Put multiply first if it isn't already.  */
6448       if (GET_CODE (XEXP (x, 1)) == MULT)
6449         {
6450           rtx tmp = XEXP (x, 0);
6451           XEXP (x, 0) = XEXP (x, 1);
6452           XEXP (x, 1) = tmp;
6453           changed = 1;
6454         }
6455
6456       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
6457          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
6458          created by virtual register instantiation, register elimination, and
6459          similar optimizations.  */
6460       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
6461         {
6462           changed = 1;
6463           x = gen_rtx_PLUS (Pmode,
6464                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
6465                                           XEXP (XEXP (x, 1), 0)),
6466                             XEXP (XEXP (x, 1), 1));
6467         }
6468
6469       /* Canonicalize
6470          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
6471          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
6472       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
6473                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6474                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
6475                && CONSTANT_P (XEXP (x, 1)))
6476         {
6477           rtx constant;
6478           rtx other = NULL_RTX;
6479
6480           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6481             {
6482               constant = XEXP (x, 1);
6483               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
6484             }
6485           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
6486             {
6487               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
6488               other = XEXP (x, 1);
6489             }
6490           else
6491             constant = 0;
6492
6493           if (constant)
6494             {
6495               changed = 1;
6496               x = gen_rtx_PLUS (Pmode,
6497                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
6498                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
6499                                 plus_constant (other, INTVAL (constant)));
6500             }
6501         }
6502
6503       if (changed && legitimate_address_p (mode, x, FALSE))
6504         return x;
6505
6506       if (GET_CODE (XEXP (x, 0)) == MULT)
6507         {
6508           changed = 1;
6509           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
6510         }
6511
6512       if (GET_CODE (XEXP (x, 1)) == MULT)
6513         {
6514           changed = 1;
6515           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
6516         }
6517
6518       if (changed
6519           && GET_CODE (XEXP (x, 1)) == REG
6520           && GET_CODE (XEXP (x, 0)) == REG)
6521         return x;
6522
6523       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
6524         {
6525           changed = 1;
6526           x = legitimize_pic_address (x, 0);
6527         }
6528
6529       if (changed && legitimate_address_p (mode, x, FALSE))
6530         return x;
6531
6532       if (GET_CODE (XEXP (x, 0)) == REG)
6533         {
6534           rtx temp = gen_reg_rtx (Pmode);
6535           rtx val  = force_operand (XEXP (x, 1), temp);
6536           if (val != temp)
6537             emit_move_insn (temp, val);
6538
6539           XEXP (x, 1) = temp;
6540           return x;
6541         }
6542
6543       else if (GET_CODE (XEXP (x, 1)) == REG)
6544         {
6545           rtx temp = gen_reg_rtx (Pmode);
6546           rtx val  = force_operand (XEXP (x, 0), temp);
6547           if (val != temp)
6548             emit_move_insn (temp, val);
6549
6550           XEXP (x, 0) = temp;
6551           return x;
6552         }
6553     }
6554
6555   return x;
6556 }
6557 \f
6558 /* Print an integer constant expression in assembler syntax.  Addition
6559    and subtraction are the only arithmetic that may appear in these
6560    expressions.  FILE is the stdio stream to write to, X is the rtx, and
6561    CODE is the operand print code from the output string.  */
6562
6563 static void
6564 output_pic_addr_const (FILE *file, rtx x, int code)
6565 {
6566   char buf[256];
6567
6568   switch (GET_CODE (x))
6569     {
6570     case PC:
6571       gcc_assert (flag_pic);
6572       putc ('.', file);
6573       break;
6574
6575     case SYMBOL_REF:
6576       assemble_name (file, XSTR (x, 0));
6577       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
6578         fputs ("@PLT", file);
6579       break;
6580
6581     case LABEL_REF:
6582       x = XEXP (x, 0);
6583       /* FALLTHRU */
6584     case CODE_LABEL:
6585       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
6586       assemble_name (asm_out_file, buf);
6587       break;
6588
6589     case CONST_INT:
6590       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
6591       break;
6592
6593     case CONST:
6594       /* This used to output parentheses around the expression,
6595          but that does not work on the 386 (either ATT or BSD assembler).  */
6596       output_pic_addr_const (file, XEXP (x, 0), code);
6597       break;
6598
6599     case CONST_DOUBLE:
6600       if (GET_MODE (x) == VOIDmode)
6601         {
6602           /* We can use %d if the number is <32 bits and positive.  */
6603           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
6604             fprintf (file, "0x%lx%08lx",
6605                      (unsigned long) CONST_DOUBLE_HIGH (x),
6606                      (unsigned long) CONST_DOUBLE_LOW (x));
6607           else
6608             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
6609         }
6610       else
6611         /* We can't handle floating point constants;
6612            PRINT_OPERAND must handle them.  */
6613         output_operand_lossage ("floating constant misused");
6614       break;
6615
6616     case PLUS:
6617       /* Some assemblers need integer constants to appear first.  */
6618       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6619         {
6620           output_pic_addr_const (file, XEXP (x, 0), code);
6621           putc ('+', file);
6622           output_pic_addr_const (file, XEXP (x, 1), code);
6623         }
6624       else 
6625         {
6626           gcc_assert (GET_CODE (XEXP (x, 1)) == CONST_INT);
6627           output_pic_addr_const (file, XEXP (x, 1), code);
6628           putc ('+', file);
6629           output_pic_addr_const (file, XEXP (x, 0), code);
6630         }
6631       break;
6632
6633     case MINUS:
6634       if (!TARGET_MACHO)
6635         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
6636       output_pic_addr_const (file, XEXP (x, 0), code);
6637       putc ('-', file);
6638       output_pic_addr_const (file, XEXP (x, 1), code);
6639       if (!TARGET_MACHO)
6640         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
6641       break;
6642
6643      case UNSPEC:
6644        gcc_assert (XVECLEN (x, 0) == 1);
6645        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
6646        switch (XINT (x, 1))
6647         {
6648         case UNSPEC_GOT:
6649           fputs ("@GOT", file);
6650           break;
6651         case UNSPEC_GOTOFF:
6652           fputs ("@GOTOFF", file);
6653           break;
6654         case UNSPEC_GOTPCREL:
6655           fputs ("@GOTPCREL(%rip)", file);
6656           break;
6657         case UNSPEC_GOTTPOFF:
6658           /* FIXME: This might be @TPOFF in Sun ld too.  */
6659           fputs ("@GOTTPOFF", file);
6660           break;
6661         case UNSPEC_TPOFF:
6662           fputs ("@TPOFF", file);
6663           break;
6664         case UNSPEC_NTPOFF:
6665           if (TARGET_64BIT)
6666             fputs ("@TPOFF", file);
6667           else
6668             fputs ("@NTPOFF", file);
6669           break;
6670         case UNSPEC_DTPOFF:
6671           fputs ("@DTPOFF", file);
6672           break;
6673         case UNSPEC_GOTNTPOFF:
6674           if (TARGET_64BIT)
6675             fputs ("@GOTTPOFF(%rip)", file);
6676           else
6677             fputs ("@GOTNTPOFF", file);
6678           break;
6679         case UNSPEC_INDNTPOFF:
6680           fputs ("@INDNTPOFF", file);
6681           break;
6682         default:
6683           output_operand_lossage ("invalid UNSPEC as operand");
6684           break;
6685         }
6686        break;
6687
6688     default:
6689       output_operand_lossage ("invalid expression as operand");
6690     }
6691 }
6692
6693 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6694    We need to emit DTP-relative relocations.  */
6695
6696 static void
6697 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
6698 {
6699   fputs (ASM_LONG, file);
6700   output_addr_const (file, x);
6701   fputs ("@DTPOFF", file);
6702   switch (size)
6703     {
6704     case 4:
6705       break;
6706     case 8:
6707       fputs (", 0", file);
6708       break;
6709     default:
6710       gcc_unreachable ();
6711    }
6712 }
6713
6714 /* In the name of slightly smaller debug output, and to cater to
6715    general assembler lossage, recognize PIC+GOTOFF and turn it back
6716    into a direct symbol reference.  */
6717
6718 static rtx
6719 ix86_delegitimize_address (rtx orig_x)
6720 {
6721   rtx x = orig_x, y;
6722
6723   if (GET_CODE (x) == MEM)
6724     x = XEXP (x, 0);
6725
6726   if (TARGET_64BIT)
6727     {
6728       if (GET_CODE (x) != CONST
6729           || GET_CODE (XEXP (x, 0)) != UNSPEC
6730           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
6731           || GET_CODE (orig_x) != MEM)
6732         return orig_x;
6733       return XVECEXP (XEXP (x, 0), 0, 0);
6734     }
6735
6736   if (GET_CODE (x) != PLUS
6737       || GET_CODE (XEXP (x, 1)) != CONST)
6738     return orig_x;
6739
6740   if (GET_CODE (XEXP (x, 0)) == REG
6741       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
6742     /* %ebx + GOT/GOTOFF */
6743     y = NULL;
6744   else if (GET_CODE (XEXP (x, 0)) == PLUS)
6745     {
6746       /* %ebx + %reg * scale + GOT/GOTOFF */
6747       y = XEXP (x, 0);
6748       if (GET_CODE (XEXP (y, 0)) == REG
6749           && REGNO (XEXP (y, 0)) == PIC_OFFSET_TABLE_REGNUM)
6750         y = XEXP (y, 1);
6751       else if (GET_CODE (XEXP (y, 1)) == REG
6752                && REGNO (XEXP (y, 1)) == PIC_OFFSET_TABLE_REGNUM)
6753         y = XEXP (y, 0);
6754       else
6755         return orig_x;
6756       if (GET_CODE (y) != REG
6757           && GET_CODE (y) != MULT
6758           && GET_CODE (y) != ASHIFT)
6759         return orig_x;
6760     }
6761   else
6762     return orig_x;
6763
6764   x = XEXP (XEXP (x, 1), 0);
6765   if (GET_CODE (x) == UNSPEC
6766       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6767           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
6768     {
6769       if (y)
6770         return gen_rtx_PLUS (Pmode, y, XVECEXP (x, 0, 0));
6771       return XVECEXP (x, 0, 0);
6772     }
6773
6774   if (GET_CODE (x) == PLUS
6775       && GET_CODE (XEXP (x, 0)) == UNSPEC
6776       && GET_CODE (XEXP (x, 1)) == CONST_INT
6777       && ((XINT (XEXP (x, 0), 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6778           || (XINT (XEXP (x, 0), 1) == UNSPEC_GOTOFF
6779               && GET_CODE (orig_x) != MEM)))
6780     {
6781       x = gen_rtx_PLUS (VOIDmode, XVECEXP (XEXP (x, 0), 0, 0), XEXP (x, 1));
6782       if (y)
6783         return gen_rtx_PLUS (Pmode, y, x);
6784       return x;
6785     }
6786
6787   return orig_x;
6788 }
6789 \f
6790 static void
6791 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
6792                     int fp, FILE *file)
6793 {
6794   const char *suffix;
6795
6796   if (mode == CCFPmode || mode == CCFPUmode)
6797     {
6798       enum rtx_code second_code, bypass_code;
6799       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
6800       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
6801       code = ix86_fp_compare_code_to_integer (code);
6802       mode = CCmode;
6803     }
6804   if (reverse)
6805     code = reverse_condition (code);
6806
6807   switch (code)
6808     {
6809     case EQ:
6810       suffix = "e";
6811       break;
6812     case NE:
6813       suffix = "ne";
6814       break;
6815     case GT:
6816       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
6817       suffix = "g";
6818       break;
6819     case GTU:
6820       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
6821          Those same assemblers have the same but opposite lossage on cmov.  */
6822       gcc_assert (mode == CCmode);
6823       suffix = fp ? "nbe" : "a";
6824       break;
6825     case LT:
6826       switch (mode)
6827         {
6828         case CCNOmode:
6829         case CCGOCmode:
6830           suffix = "s";
6831           break;
6832
6833         case CCmode:
6834         case CCGCmode:
6835           suffix = "l";
6836           break;
6837
6838         default:
6839           gcc_unreachable ();
6840         }
6841       break;
6842     case LTU:
6843       gcc_assert (mode == CCmode);
6844       suffix = "b";
6845       break;
6846     case GE:
6847       switch (mode)
6848         {
6849         case CCNOmode:
6850         case CCGOCmode:
6851           suffix = "ns";
6852           break;
6853
6854         case CCmode:
6855         case CCGCmode:
6856           suffix = "ge";
6857           break;
6858
6859         default:
6860           gcc_unreachable ();
6861         }
6862       break;
6863     case GEU:
6864       /* ??? As above.  */
6865       gcc_assert (mode == CCmode);
6866       suffix = fp ? "nb" : "ae";
6867       break;
6868     case LE:
6869       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
6870       suffix = "le";
6871       break;
6872     case LEU:
6873       gcc_assert (mode == CCmode);
6874       suffix = "be";
6875       break;
6876     case UNORDERED:
6877       suffix = fp ? "u" : "p";
6878       break;
6879     case ORDERED:
6880       suffix = fp ? "nu" : "np";
6881       break;
6882     default:
6883       gcc_unreachable ();
6884     }
6885   fputs (suffix, file);
6886 }
6887
6888 /* Print the name of register X to FILE based on its machine mode and number.
6889    If CODE is 'w', pretend the mode is HImode.
6890    If CODE is 'b', pretend the mode is QImode.
6891    If CODE is 'k', pretend the mode is SImode.
6892    If CODE is 'q', pretend the mode is DImode.
6893    If CODE is 'h', pretend the reg is the 'high' byte register.
6894    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
6895
6896 void
6897 print_reg (rtx x, int code, FILE *file)
6898 {
6899   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
6900               && REGNO (x) != FRAME_POINTER_REGNUM
6901               && REGNO (x) != FLAGS_REG
6902               && REGNO (x) != FPSR_REG);
6903
6904   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
6905     putc ('%', file);
6906
6907   if (code == 'w' || MMX_REG_P (x))
6908     code = 2;
6909   else if (code == 'b')
6910     code = 1;
6911   else if (code == 'k')
6912     code = 4;
6913   else if (code == 'q')
6914     code = 8;
6915   else if (code == 'y')
6916     code = 3;
6917   else if (code == 'h')
6918     code = 0;
6919   else
6920     code = GET_MODE_SIZE (GET_MODE (x));
6921
6922   /* Irritatingly, AMD extended registers use different naming convention
6923      from the normal registers.  */
6924   if (REX_INT_REG_P (x))
6925     {
6926       gcc_assert (TARGET_64BIT);
6927       switch (code)
6928         {
6929           case 0:
6930             error ("extended registers have no high halves");
6931             break;
6932           case 1:
6933             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
6934             break;
6935           case 2:
6936             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
6937             break;
6938           case 4:
6939             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
6940             break;
6941           case 8:
6942             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
6943             break;
6944           default:
6945             error ("unsupported operand size for extended register");
6946             break;
6947         }
6948       return;
6949     }
6950   switch (code)
6951     {
6952     case 3:
6953       if (STACK_TOP_P (x))
6954         {
6955           fputs ("st(0)", file);
6956           break;
6957         }
6958       /* FALLTHRU */
6959     case 8:
6960     case 4:
6961     case 12:
6962       if (! ANY_FP_REG_P (x))
6963         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
6964       /* FALLTHRU */
6965     case 16:
6966     case 2:
6967     normal:
6968       fputs (hi_reg_name[REGNO (x)], file);
6969       break;
6970     case 1:
6971       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
6972         goto normal;
6973       fputs (qi_reg_name[REGNO (x)], file);
6974       break;
6975     case 0:
6976       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
6977         goto normal;
6978       fputs (qi_high_reg_name[REGNO (x)], file);
6979       break;
6980     default:
6981       gcc_unreachable ();
6982     }
6983 }
6984
6985 /* Locate some local-dynamic symbol still in use by this function
6986    so that we can print its name in some tls_local_dynamic_base
6987    pattern.  */
6988
6989 static const char *
6990 get_some_local_dynamic_name (void)
6991 {
6992   rtx insn;
6993
6994   if (cfun->machine->some_ld_name)
6995     return cfun->machine->some_ld_name;
6996
6997   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
6998     if (INSN_P (insn)
6999         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
7000       return cfun->machine->some_ld_name;
7001
7002   gcc_unreachable ();
7003 }
7004
7005 static int
7006 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
7007 {
7008   rtx x = *px;
7009
7010   if (GET_CODE (x) == SYMBOL_REF
7011       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
7012     {
7013       cfun->machine->some_ld_name = XSTR (x, 0);
7014       return 1;
7015     }
7016
7017   return 0;
7018 }
7019
7020 /* Meaning of CODE:
7021    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
7022    C -- print opcode suffix for set/cmov insn.
7023    c -- like C, but print reversed condition
7024    F,f -- likewise, but for floating-point.
7025    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
7026         otherwise nothing
7027    R -- print the prefix for register names.
7028    z -- print the opcode suffix for the size of the current operand.
7029    * -- print a star (in certain assembler syntax)
7030    A -- print an absolute memory reference.
7031    w -- print the operand as if it's a "word" (HImode) even if it isn't.
7032    s -- print a shift double count, followed by the assemblers argument
7033         delimiter.
7034    b -- print the QImode name of the register for the indicated operand.
7035         %b0 would print %al if operands[0] is reg 0.
7036    w --  likewise, print the HImode name of the register.
7037    k --  likewise, print the SImode name of the register.
7038    q --  likewise, print the DImode name of the register.
7039    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
7040    y -- print "st(0)" instead of "st" as a register.
7041    D -- print condition for SSE cmp instruction.
7042    P -- if PIC, print an @PLT suffix.
7043    X -- don't print any sort of PIC '@' suffix for a symbol.
7044    & -- print some in-use local-dynamic symbol name.
7045    H -- print a memory address offset by 8; used for sse high-parts
7046  */
7047
7048 void
7049 print_operand (FILE *file, rtx x, int code)
7050 {
7051   if (code)
7052     {
7053       switch (code)
7054         {
7055         case '*':
7056           if (ASSEMBLER_DIALECT == ASM_ATT)
7057             putc ('*', file);
7058           return;
7059
7060         case '&':
7061           assemble_name (file, get_some_local_dynamic_name ());
7062           return;
7063
7064         case 'A':
7065           switch (ASSEMBLER_DIALECT)
7066             {
7067             case ASM_ATT:
7068               putc ('*', file);
7069               break;
7070
7071             case ASM_INTEL:
7072               /* Intel syntax. For absolute addresses, registers should not
7073                  be surrounded by braces.  */
7074               if (GET_CODE (x) != REG)
7075                 {
7076                   putc ('[', file);
7077                   PRINT_OPERAND (file, x, 0);
7078                   putc (']', file);
7079                   return;
7080                 }
7081               break;
7082
7083             default:
7084               gcc_unreachable ();
7085             }
7086
7087           PRINT_OPERAND (file, x, 0);
7088           return;
7089
7090
7091         case 'L':
7092           if (ASSEMBLER_DIALECT == ASM_ATT)
7093             putc ('l', file);
7094           return;
7095
7096         case 'W':
7097           if (ASSEMBLER_DIALECT == ASM_ATT)
7098             putc ('w', file);
7099           return;
7100
7101         case 'B':
7102           if (ASSEMBLER_DIALECT == ASM_ATT)
7103             putc ('b', file);
7104           return;
7105
7106         case 'Q':
7107           if (ASSEMBLER_DIALECT == ASM_ATT)
7108             putc ('l', file);
7109           return;
7110
7111         case 'S':
7112           if (ASSEMBLER_DIALECT == ASM_ATT)
7113             putc ('s', file);
7114           return;
7115
7116         case 'T':
7117           if (ASSEMBLER_DIALECT == ASM_ATT)
7118             putc ('t', file);
7119           return;
7120
7121         case 'z':
7122           /* 387 opcodes don't get size suffixes if the operands are
7123              registers.  */
7124           if (STACK_REG_P (x))
7125             return;
7126
7127           /* Likewise if using Intel opcodes.  */
7128           if (ASSEMBLER_DIALECT == ASM_INTEL)
7129             return;
7130
7131           /* This is the size of op from size of operand.  */
7132           switch (GET_MODE_SIZE (GET_MODE (x)))
7133             {
7134             case 2:
7135 #ifdef HAVE_GAS_FILDS_FISTS
7136               putc ('s', file);
7137 #endif
7138               return;
7139
7140             case 4:
7141               if (GET_MODE (x) == SFmode)
7142                 {
7143                   putc ('s', file);
7144                   return;
7145                 }
7146               else
7147                 putc ('l', file);
7148               return;
7149
7150             case 12:
7151             case 16:
7152               putc ('t', file);
7153               return;
7154
7155             case 8:
7156               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
7157                 {
7158 #ifdef GAS_MNEMONICS
7159                   putc ('q', file);
7160 #else
7161                   putc ('l', file);
7162                   putc ('l', file);
7163 #endif
7164                 }
7165               else
7166                 putc ('l', file);
7167               return;
7168
7169             default:
7170               gcc_unreachable ();
7171             }
7172
7173         case 'b':
7174         case 'w':
7175         case 'k':
7176         case 'q':
7177         case 'h':
7178         case 'y':
7179         case 'X':
7180         case 'P':
7181           break;
7182
7183         case 's':
7184           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
7185             {
7186               PRINT_OPERAND (file, x, 0);
7187               putc (',', file);
7188             }
7189           return;
7190
7191         case 'D':
7192           /* Little bit of braindamage here.  The SSE compare instructions
7193              does use completely different names for the comparisons that the
7194              fp conditional moves.  */
7195           switch (GET_CODE (x))
7196             {
7197             case EQ:
7198             case UNEQ:
7199               fputs ("eq", file);
7200               break;
7201             case LT:
7202             case UNLT:
7203               fputs ("lt", file);
7204               break;
7205             case LE:
7206             case UNLE:
7207               fputs ("le", file);
7208               break;
7209             case UNORDERED:
7210               fputs ("unord", file);
7211               break;
7212             case NE:
7213             case LTGT:
7214               fputs ("neq", file);
7215               break;
7216             case UNGE:
7217             case GE:
7218               fputs ("nlt", file);
7219               break;
7220             case UNGT:
7221             case GT:
7222               fputs ("nle", file);
7223               break;
7224             case ORDERED:
7225               fputs ("ord", file);
7226               break;
7227             default:
7228               gcc_unreachable ();
7229             }
7230           return;
7231         case 'O':
7232 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7233           if (ASSEMBLER_DIALECT == ASM_ATT)
7234             {
7235               switch (GET_MODE (x))
7236                 {
7237                 case HImode: putc ('w', file); break;
7238                 case SImode:
7239                 case SFmode: putc ('l', file); break;
7240                 case DImode:
7241                 case DFmode: putc ('q', file); break;
7242                 default: gcc_unreachable ();
7243                 }
7244               putc ('.', file);
7245             }
7246 #endif
7247           return;
7248         case 'C':
7249           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
7250           return;
7251         case 'F':
7252 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7253           if (ASSEMBLER_DIALECT == ASM_ATT)
7254             putc ('.', file);
7255 #endif
7256           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
7257           return;
7258
7259           /* Like above, but reverse condition */
7260         case 'c':
7261           /* Check to see if argument to %c is really a constant
7262              and not a condition code which needs to be reversed.  */
7263           if (!COMPARISON_P (x))
7264           {
7265             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
7266              return;
7267           }
7268           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
7269           return;
7270         case 'f':
7271 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7272           if (ASSEMBLER_DIALECT == ASM_ATT)
7273             putc ('.', file);
7274 #endif
7275           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
7276           return;
7277
7278         case 'H':
7279           /* It doesn't actually matter what mode we use here, as we're
7280              only going to use this for printing.  */
7281           x = adjust_address_nv (x, DImode, 8);
7282           break;
7283
7284         case '+':
7285           {
7286             rtx x;
7287
7288             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
7289               return;
7290
7291             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
7292             if (x)
7293               {
7294                 int pred_val = INTVAL (XEXP (x, 0));
7295
7296                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
7297                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
7298                   {
7299                     int taken = pred_val > REG_BR_PROB_BASE / 2;
7300                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
7301
7302                     /* Emit hints only in the case default branch prediction
7303                        heuristics would fail.  */
7304                     if (taken != cputaken)
7305                       {
7306                         /* We use 3e (DS) prefix for taken branches and
7307                            2e (CS) prefix for not taken branches.  */
7308                         if (taken)
7309                           fputs ("ds ; ", file);
7310                         else
7311                           fputs ("cs ; ", file);
7312                       }
7313                   }
7314               }
7315             return;
7316           }
7317         default:
7318             output_operand_lossage ("invalid operand code '%c'", code);
7319         }
7320     }
7321
7322   if (GET_CODE (x) == REG)
7323     print_reg (x, code, file);
7324
7325   else if (GET_CODE (x) == MEM)
7326     {
7327       /* No `byte ptr' prefix for call instructions.  */
7328       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
7329         {
7330           const char * size;
7331           switch (GET_MODE_SIZE (GET_MODE (x)))
7332             {
7333             case 1: size = "BYTE"; break;
7334             case 2: size = "WORD"; break;
7335             case 4: size = "DWORD"; break;
7336             case 8: size = "QWORD"; break;
7337             case 12: size = "XWORD"; break;
7338             case 16: size = "XMMWORD"; break;
7339             default:
7340               gcc_unreachable ();
7341             }
7342
7343           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
7344           if (code == 'b')
7345             size = "BYTE";
7346           else if (code == 'w')
7347             size = "WORD";
7348           else if (code == 'k')
7349             size = "DWORD";
7350
7351           fputs (size, file);
7352           fputs (" PTR ", file);
7353         }
7354
7355       x = XEXP (x, 0);
7356       /* Avoid (%rip) for call operands.  */
7357       if (CONSTANT_ADDRESS_P (x) && code == 'P'
7358                && GET_CODE (x) != CONST_INT)
7359         output_addr_const (file, x);
7360       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
7361         output_operand_lossage ("invalid constraints for operand");
7362       else
7363         output_address (x);
7364     }
7365
7366   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
7367     {
7368       REAL_VALUE_TYPE r;
7369       long l;
7370
7371       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7372       REAL_VALUE_TO_TARGET_SINGLE (r, l);
7373
7374       if (ASSEMBLER_DIALECT == ASM_ATT)
7375         putc ('$', file);
7376       fprintf (file, "0x%08lx", l);
7377     }
7378
7379   /* These float cases don't actually occur as immediate operands.  */
7380   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
7381     {
7382       char dstr[30];
7383
7384       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7385       fprintf (file, "%s", dstr);
7386     }
7387
7388   else if (GET_CODE (x) == CONST_DOUBLE
7389            && GET_MODE (x) == XFmode)
7390     {
7391       char dstr[30];
7392
7393       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7394       fprintf (file, "%s", dstr);
7395     }
7396
7397   else
7398     {
7399       /* We have patterns that allow zero sets of memory, for instance.
7400          In 64-bit mode, we should probably support all 8-byte vectors,
7401          since we can in fact encode that into an immediate.  */
7402       if (GET_CODE (x) == CONST_VECTOR)
7403         {
7404           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
7405           x = const0_rtx;
7406         }
7407
7408       if (code != 'P')
7409         {
7410           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
7411             {
7412               if (ASSEMBLER_DIALECT == ASM_ATT)
7413                 putc ('$', file);
7414             }
7415           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
7416                    || GET_CODE (x) == LABEL_REF)
7417             {
7418               if (ASSEMBLER_DIALECT == ASM_ATT)
7419                 putc ('$', file);
7420               else
7421                 fputs ("OFFSET FLAT:", file);
7422             }
7423         }
7424       if (GET_CODE (x) == CONST_INT)
7425         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7426       else if (flag_pic)
7427         output_pic_addr_const (file, x, code);
7428       else
7429         output_addr_const (file, x);
7430     }
7431 }
7432 \f
7433 /* Print a memory operand whose address is ADDR.  */
7434
7435 void
7436 print_operand_address (FILE *file, rtx addr)
7437 {
7438   struct ix86_address parts;
7439   rtx base, index, disp;
7440   int scale;
7441   int ok = ix86_decompose_address (addr, &parts);
7442
7443   gcc_assert (ok);
7444
7445   base = parts.base;
7446   index = parts.index;
7447   disp = parts.disp;
7448   scale = parts.scale;
7449
7450   switch (parts.seg)
7451     {
7452     case SEG_DEFAULT:
7453       break;
7454     case SEG_FS:
7455     case SEG_GS:
7456       if (USER_LABEL_PREFIX[0] == 0)
7457         putc ('%', file);
7458       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
7459       break;
7460     default:
7461       gcc_unreachable ();
7462     }
7463
7464   if (!base && !index)
7465     {
7466       /* Displacement only requires special attention.  */
7467
7468       if (GET_CODE (disp) == CONST_INT)
7469         {
7470           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
7471             {
7472               if (USER_LABEL_PREFIX[0] == 0)
7473                 putc ('%', file);
7474               fputs ("ds:", file);
7475             }
7476           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
7477         }
7478       else if (flag_pic)
7479         output_pic_addr_const (file, disp, 0);
7480       else
7481         output_addr_const (file, disp);
7482
7483       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
7484       if (TARGET_64BIT)
7485         {
7486           if (GET_CODE (disp) == CONST
7487               && GET_CODE (XEXP (disp, 0)) == PLUS
7488               && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
7489             disp = XEXP (XEXP (disp, 0), 0);
7490           if (GET_CODE (disp) == LABEL_REF
7491               || (GET_CODE (disp) == SYMBOL_REF
7492                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
7493             fputs ("(%rip)", file);
7494         }
7495     }
7496   else
7497     {
7498       if (ASSEMBLER_DIALECT == ASM_ATT)
7499         {
7500           if (disp)
7501             {
7502               if (flag_pic)
7503                 output_pic_addr_const (file, disp, 0);
7504               else if (GET_CODE (disp) == LABEL_REF)
7505                 output_asm_label (disp);
7506               else
7507                 output_addr_const (file, disp);
7508             }
7509
7510           putc ('(', file);
7511           if (base)
7512             print_reg (base, 0, file);
7513           if (index)
7514             {
7515               putc (',', file);
7516               print_reg (index, 0, file);
7517               if (scale != 1)
7518                 fprintf (file, ",%d", scale);
7519             }
7520           putc (')', file);
7521         }
7522       else
7523         {
7524           rtx offset = NULL_RTX;
7525
7526           if (disp)
7527             {
7528               /* Pull out the offset of a symbol; print any symbol itself.  */
7529               if (GET_CODE (disp) == CONST
7530                   && GET_CODE (XEXP (disp, 0)) == PLUS
7531                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
7532                 {
7533                   offset = XEXP (XEXP (disp, 0), 1);
7534                   disp = gen_rtx_CONST (VOIDmode,
7535                                         XEXP (XEXP (disp, 0), 0));
7536                 }
7537
7538               if (flag_pic)
7539                 output_pic_addr_const (file, disp, 0);
7540               else if (GET_CODE (disp) == LABEL_REF)
7541                 output_asm_label (disp);
7542               else if (GET_CODE (disp) == CONST_INT)
7543                 offset = disp;
7544               else
7545                 output_addr_const (file, disp);
7546             }
7547
7548           putc ('[', file);
7549           if (base)
7550             {
7551               print_reg (base, 0, file);
7552               if (offset)
7553                 {
7554                   if (INTVAL (offset) >= 0)
7555                     putc ('+', file);
7556                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7557                 }
7558             }
7559           else if (offset)
7560             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7561           else
7562             putc ('0', file);
7563
7564           if (index)
7565             {
7566               putc ('+', file);
7567               print_reg (index, 0, file);
7568               if (scale != 1)
7569                 fprintf (file, "*%d", scale);
7570             }
7571           putc (']', file);
7572         }
7573     }
7574 }
7575
7576 bool
7577 output_addr_const_extra (FILE *file, rtx x)
7578 {
7579   rtx op;
7580
7581   if (GET_CODE (x) != UNSPEC)
7582     return false;
7583
7584   op = XVECEXP (x, 0, 0);
7585   switch (XINT (x, 1))
7586     {
7587     case UNSPEC_GOTTPOFF:
7588       output_addr_const (file, op);
7589       /* FIXME: This might be @TPOFF in Sun ld.  */
7590       fputs ("@GOTTPOFF", file);
7591       break;
7592     case UNSPEC_TPOFF:
7593       output_addr_const (file, op);
7594       fputs ("@TPOFF", file);
7595       break;
7596     case UNSPEC_NTPOFF:
7597       output_addr_const (file, op);
7598       if (TARGET_64BIT)
7599         fputs ("@TPOFF", file);
7600       else
7601         fputs ("@NTPOFF", file);
7602       break;
7603     case UNSPEC_DTPOFF:
7604       output_addr_const (file, op);
7605       fputs ("@DTPOFF", file);
7606       break;
7607     case UNSPEC_GOTNTPOFF:
7608       output_addr_const (file, op);
7609       if (TARGET_64BIT)
7610         fputs ("@GOTTPOFF(%rip)", file);
7611       else
7612         fputs ("@GOTNTPOFF", file);
7613       break;
7614     case UNSPEC_INDNTPOFF:
7615       output_addr_const (file, op);
7616       fputs ("@INDNTPOFF", file);
7617       break;
7618
7619     default:
7620       return false;
7621     }
7622
7623   return true;
7624 }
7625 \f
7626 /* Split one or more DImode RTL references into pairs of SImode
7627    references.  The RTL can be REG, offsettable MEM, integer constant, or
7628    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7629    split and "num" is its length.  lo_half and hi_half are output arrays
7630    that parallel "operands".  */
7631
7632 void
7633 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
7634 {
7635   while (num--)
7636     {
7637       rtx op = operands[num];
7638
7639       /* simplify_subreg refuse to split volatile memory addresses,
7640          but we still have to handle it.  */
7641       if (GET_CODE (op) == MEM)
7642         {
7643           lo_half[num] = adjust_address (op, SImode, 0);
7644           hi_half[num] = adjust_address (op, SImode, 4);
7645         }
7646       else
7647         {
7648           lo_half[num] = simplify_gen_subreg (SImode, op,
7649                                               GET_MODE (op) == VOIDmode
7650                                               ? DImode : GET_MODE (op), 0);
7651           hi_half[num] = simplify_gen_subreg (SImode, op,
7652                                               GET_MODE (op) == VOIDmode
7653                                               ? DImode : GET_MODE (op), 4);
7654         }
7655     }
7656 }
7657 /* Split one or more TImode RTL references into pairs of DImode
7658    references.  The RTL can be REG, offsettable MEM, integer constant, or
7659    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7660    split and "num" is its length.  lo_half and hi_half are output arrays
7661    that parallel "operands".  */
7662
7663 void
7664 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
7665 {
7666   while (num--)
7667     {
7668       rtx op = operands[num];
7669
7670       /* simplify_subreg refuse to split volatile memory addresses, but we
7671          still have to handle it.  */
7672       if (GET_CODE (op) == MEM)
7673         {
7674           lo_half[num] = adjust_address (op, DImode, 0);
7675           hi_half[num] = adjust_address (op, DImode, 8);
7676         }
7677       else
7678         {
7679           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
7680           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
7681         }
7682     }
7683 }
7684 \f
7685 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
7686    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
7687    is the expression of the binary operation.  The output may either be
7688    emitted here, or returned to the caller, like all output_* functions.
7689
7690    There is no guarantee that the operands are the same mode, as they
7691    might be within FLOAT or FLOAT_EXTEND expressions.  */
7692
7693 #ifndef SYSV386_COMPAT
7694 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
7695    wants to fix the assemblers because that causes incompatibility
7696    with gcc.  No-one wants to fix gcc because that causes
7697    incompatibility with assemblers...  You can use the option of
7698    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
7699 #define SYSV386_COMPAT 1
7700 #endif
7701
7702 const char *
7703 output_387_binary_op (rtx insn, rtx *operands)
7704 {
7705   static char buf[30];
7706   const char *p;
7707   const char *ssep;
7708   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
7709
7710 #ifdef ENABLE_CHECKING
7711   /* Even if we do not want to check the inputs, this documents input
7712      constraints.  Which helps in understanding the following code.  */
7713   if (STACK_REG_P (operands[0])
7714       && ((REG_P (operands[1])
7715            && REGNO (operands[0]) == REGNO (operands[1])
7716            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
7717           || (REG_P (operands[2])
7718               && REGNO (operands[0]) == REGNO (operands[2])
7719               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
7720       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
7721     ; /* ok */
7722   else
7723     gcc_assert (is_sse);
7724 #endif
7725
7726   switch (GET_CODE (operands[3]))
7727     {
7728     case PLUS:
7729       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7730           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7731         p = "fiadd";
7732       else
7733         p = "fadd";
7734       ssep = "add";
7735       break;
7736
7737     case MINUS:
7738       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7739           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7740         p = "fisub";
7741       else
7742         p = "fsub";
7743       ssep = "sub";
7744       break;
7745
7746     case MULT:
7747       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7748           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7749         p = "fimul";
7750       else
7751         p = "fmul";
7752       ssep = "mul";
7753       break;
7754
7755     case DIV:
7756       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7757           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7758         p = "fidiv";
7759       else
7760         p = "fdiv";
7761       ssep = "div";
7762       break;
7763
7764     default:
7765       gcc_unreachable ();
7766     }
7767
7768   if (is_sse)
7769    {
7770       strcpy (buf, ssep);
7771       if (GET_MODE (operands[0]) == SFmode)
7772         strcat (buf, "ss\t{%2, %0|%0, %2}");
7773       else
7774         strcat (buf, "sd\t{%2, %0|%0, %2}");
7775       return buf;
7776    }
7777   strcpy (buf, p);
7778
7779   switch (GET_CODE (operands[3]))
7780     {
7781     case MULT:
7782     case PLUS:
7783       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
7784         {
7785           rtx temp = operands[2];
7786           operands[2] = operands[1];
7787           operands[1] = temp;
7788         }
7789
7790       /* know operands[0] == operands[1].  */
7791
7792       if (GET_CODE (operands[2]) == MEM)
7793         {
7794           p = "%z2\t%2";
7795           break;
7796         }
7797
7798       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7799         {
7800           if (STACK_TOP_P (operands[0]))
7801             /* How is it that we are storing to a dead operand[2]?
7802                Well, presumably operands[1] is dead too.  We can't
7803                store the result to st(0) as st(0) gets popped on this
7804                instruction.  Instead store to operands[2] (which I
7805                think has to be st(1)).  st(1) will be popped later.
7806                gcc <= 2.8.1 didn't have this check and generated
7807                assembly code that the Unixware assembler rejected.  */
7808             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
7809           else
7810             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7811           break;
7812         }
7813
7814       if (STACK_TOP_P (operands[0]))
7815         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
7816       else
7817         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
7818       break;
7819
7820     case MINUS:
7821     case DIV:
7822       if (GET_CODE (operands[1]) == MEM)
7823         {
7824           p = "r%z1\t%1";
7825           break;
7826         }
7827
7828       if (GET_CODE (operands[2]) == MEM)
7829         {
7830           p = "%z2\t%2";
7831           break;
7832         }
7833
7834       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7835         {
7836 #if SYSV386_COMPAT
7837           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
7838              derived assemblers, confusingly reverse the direction of
7839              the operation for fsub{r} and fdiv{r} when the
7840              destination register is not st(0).  The Intel assembler
7841              doesn't have this brain damage.  Read !SYSV386_COMPAT to
7842              figure out what the hardware really does.  */
7843           if (STACK_TOP_P (operands[0]))
7844             p = "{p\t%0, %2|rp\t%2, %0}";
7845           else
7846             p = "{rp\t%2, %0|p\t%0, %2}";
7847 #else
7848           if (STACK_TOP_P (operands[0]))
7849             /* As above for fmul/fadd, we can't store to st(0).  */
7850             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
7851           else
7852             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7853 #endif
7854           break;
7855         }
7856
7857       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
7858         {
7859 #if SYSV386_COMPAT
7860           if (STACK_TOP_P (operands[0]))
7861             p = "{rp\t%0, %1|p\t%1, %0}";
7862           else
7863             p = "{p\t%1, %0|rp\t%0, %1}";
7864 #else
7865           if (STACK_TOP_P (operands[0]))
7866             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
7867           else
7868             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
7869 #endif
7870           break;
7871         }
7872
7873       if (STACK_TOP_P (operands[0]))
7874         {
7875           if (STACK_TOP_P (operands[1]))
7876             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
7877           else
7878             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
7879           break;
7880         }
7881       else if (STACK_TOP_P (operands[1]))
7882         {
7883 #if SYSV386_COMPAT
7884           p = "{\t%1, %0|r\t%0, %1}";
7885 #else
7886           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
7887 #endif
7888         }
7889       else
7890         {
7891 #if SYSV386_COMPAT
7892           p = "{r\t%2, %0|\t%0, %2}";
7893 #else
7894           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
7895 #endif
7896         }
7897       break;
7898
7899     default:
7900       gcc_unreachable ();
7901     }
7902
7903   strcat (buf, p);
7904   return buf;
7905 }
7906
7907 /* Return needed mode for entity in optimize_mode_switching pass.  */
7908
7909 int
7910 ix86_mode_needed (int entity, rtx insn)
7911 {
7912   enum attr_i387_cw mode;
7913
7914   /* The mode UNINITIALIZED is used to store control word after a
7915      function call or ASM pattern.  The mode ANY specify that function
7916      has no requirements on the control word and make no changes in the
7917      bits we are interested in.  */
7918
7919   if (CALL_P (insn)
7920       || (NONJUMP_INSN_P (insn)
7921           && (asm_noperands (PATTERN (insn)) >= 0
7922               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
7923     return I387_CW_UNINITIALIZED;
7924
7925   if (recog_memoized (insn) < 0)
7926     return I387_CW_ANY;
7927
7928   mode = get_attr_i387_cw (insn);
7929
7930   switch (entity)
7931     {
7932     case I387_TRUNC:
7933       if (mode == I387_CW_TRUNC)
7934         return mode;
7935       break;
7936
7937     case I387_FLOOR:
7938       if (mode == I387_CW_FLOOR)
7939         return mode;
7940       break;
7941
7942     case I387_CEIL:
7943       if (mode == I387_CW_CEIL)
7944         return mode;
7945       break;
7946
7947     case I387_MASK_PM:
7948       if (mode == I387_CW_MASK_PM)
7949         return mode;
7950       break;
7951
7952     default:
7953       gcc_unreachable ();
7954     }
7955
7956   return I387_CW_ANY;
7957 }
7958
7959 /* Output code to initialize control word copies used by trunc?f?i and
7960    rounding patterns.  CURRENT_MODE is set to current control word,
7961    while NEW_MODE is set to new control word.  */
7962
7963 void
7964 emit_i387_cw_initialization (int mode)
7965 {
7966   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
7967   rtx new_mode;
7968
7969   int slot;
7970
7971   rtx reg = gen_reg_rtx (HImode);
7972
7973   emit_insn (gen_x86_fnstcw_1 (stored_mode));
7974   emit_move_insn (reg, stored_mode);
7975
7976   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
7977     {
7978       switch (mode)
7979         {
7980         case I387_CW_TRUNC:
7981           /* round toward zero (truncate) */
7982           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
7983           slot = SLOT_CW_TRUNC;
7984           break;
7985
7986         case I387_CW_FLOOR:
7987           /* round down toward -oo */
7988           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
7989           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
7990           slot = SLOT_CW_FLOOR;
7991           break;
7992
7993         case I387_CW_CEIL:
7994           /* round up toward +oo */
7995           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
7996           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
7997           slot = SLOT_CW_CEIL;
7998           break;
7999
8000         case I387_CW_MASK_PM:
8001           /* mask precision exception for nearbyint() */
8002           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8003           slot = SLOT_CW_MASK_PM;
8004           break;
8005
8006         default:
8007           gcc_unreachable ();
8008         }
8009     }
8010   else
8011     {
8012       switch (mode)
8013         {
8014         case I387_CW_TRUNC:
8015           /* round toward zero (truncate) */
8016           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
8017           slot = SLOT_CW_TRUNC;
8018           break;
8019
8020         case I387_CW_FLOOR:
8021           /* round down toward -oo */
8022           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
8023           slot = SLOT_CW_FLOOR;
8024           break;
8025
8026         case I387_CW_CEIL:
8027           /* round up toward +oo */
8028           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
8029           slot = SLOT_CW_CEIL;
8030           break;
8031  
8032         case I387_CW_MASK_PM:
8033           /* mask precision exception for nearbyint() */
8034           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8035           slot = SLOT_CW_MASK_PM;
8036           break;
8037
8038         default:
8039           gcc_unreachable ();
8040         }
8041     }
8042
8043   gcc_assert (slot < MAX_386_STACK_LOCALS);
8044
8045   new_mode = assign_386_stack_local (HImode, slot);
8046   emit_move_insn (new_mode, reg);
8047 }
8048
8049 /* Output code for INSN to convert a float to a signed int.  OPERANDS
8050    are the insn operands.  The output may be [HSD]Imode and the input
8051    operand may be [SDX]Fmode.  */
8052
8053 const char *
8054 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
8055 {
8056   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8057   int dimode_p = GET_MODE (operands[0]) == DImode;
8058   int round_mode = get_attr_i387_cw (insn);
8059
8060   /* Jump through a hoop or two for DImode, since the hardware has no
8061      non-popping instruction.  We used to do this a different way, but
8062      that was somewhat fragile and broke with post-reload splitters.  */
8063   if ((dimode_p || fisttp) && !stack_top_dies)
8064     output_asm_insn ("fld\t%y1", operands);
8065
8066   gcc_assert (STACK_TOP_P (operands[1]));
8067   gcc_assert (GET_CODE (operands[0]) == MEM);
8068
8069   if (fisttp)
8070       output_asm_insn ("fisttp%z0\t%0", operands);
8071   else
8072     {
8073       if (round_mode != I387_CW_ANY)
8074         output_asm_insn ("fldcw\t%3", operands);
8075       if (stack_top_dies || dimode_p)
8076         output_asm_insn ("fistp%z0\t%0", operands);
8077       else
8078         output_asm_insn ("fist%z0\t%0", operands);
8079       if (round_mode != I387_CW_ANY)
8080         output_asm_insn ("fldcw\t%2", operands);
8081     }
8082
8083   return "";
8084 }
8085
8086 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
8087    should be used.  UNORDERED_P is true when fucom should be used.  */
8088
8089 const char *
8090 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
8091 {
8092   int stack_top_dies;
8093   rtx cmp_op0, cmp_op1;
8094   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
8095
8096   if (eflags_p)
8097     {
8098       cmp_op0 = operands[0];
8099       cmp_op1 = operands[1];
8100     }
8101   else
8102     {
8103       cmp_op0 = operands[1];
8104       cmp_op1 = operands[2];
8105     }
8106
8107   if (is_sse)
8108     {
8109       if (GET_MODE (operands[0]) == SFmode)
8110         if (unordered_p)
8111           return "ucomiss\t{%1, %0|%0, %1}";
8112         else
8113           return "comiss\t{%1, %0|%0, %1}";
8114       else
8115         if (unordered_p)
8116           return "ucomisd\t{%1, %0|%0, %1}";
8117         else
8118           return "comisd\t{%1, %0|%0, %1}";
8119     }
8120
8121   gcc_assert (STACK_TOP_P (cmp_op0));
8122
8123   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8124
8125   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
8126     {
8127       if (stack_top_dies)
8128         {
8129           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
8130           return TARGET_USE_FFREEP ? "ffreep\t%y1" : "fstp\t%y1";
8131         }
8132       else
8133         return "ftst\n\tfnstsw\t%0";
8134     }
8135
8136   if (STACK_REG_P (cmp_op1)
8137       && stack_top_dies
8138       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
8139       && REGNO (cmp_op1) != FIRST_STACK_REG)
8140     {
8141       /* If both the top of the 387 stack dies, and the other operand
8142          is also a stack register that dies, then this must be a
8143          `fcompp' float compare */
8144
8145       if (eflags_p)
8146         {
8147           /* There is no double popping fcomi variant.  Fortunately,
8148              eflags is immune from the fstp's cc clobbering.  */
8149           if (unordered_p)
8150             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
8151           else
8152             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
8153           return TARGET_USE_FFREEP ? "ffreep\t%y0" : "fstp\t%y0";
8154         }
8155       else
8156         {
8157           if (unordered_p)
8158             return "fucompp\n\tfnstsw\t%0";
8159           else
8160             return "fcompp\n\tfnstsw\t%0";
8161         }
8162     }
8163   else
8164     {
8165       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
8166
8167       static const char * const alt[16] =
8168       {
8169         "fcom%z2\t%y2\n\tfnstsw\t%0",
8170         "fcomp%z2\t%y2\n\tfnstsw\t%0",
8171         "fucom%z2\t%y2\n\tfnstsw\t%0",
8172         "fucomp%z2\t%y2\n\tfnstsw\t%0",
8173
8174         "ficom%z2\t%y2\n\tfnstsw\t%0",
8175         "ficomp%z2\t%y2\n\tfnstsw\t%0",
8176         NULL,
8177         NULL,
8178
8179         "fcomi\t{%y1, %0|%0, %y1}",
8180         "fcomip\t{%y1, %0|%0, %y1}",
8181         "fucomi\t{%y1, %0|%0, %y1}",
8182         "fucomip\t{%y1, %0|%0, %y1}",
8183
8184         NULL,
8185         NULL,
8186         NULL,
8187         NULL
8188       };
8189
8190       int mask;
8191       const char *ret;
8192
8193       mask  = eflags_p << 3;
8194       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
8195       mask |= unordered_p << 1;
8196       mask |= stack_top_dies;
8197
8198       gcc_assert (mask < 16);
8199       ret = alt[mask];
8200       gcc_assert (ret);
8201
8202       return ret;
8203     }
8204 }
8205
8206 void
8207 ix86_output_addr_vec_elt (FILE *file, int value)
8208 {
8209   const char *directive = ASM_LONG;
8210
8211 #ifdef ASM_QUAD
8212   if (TARGET_64BIT)
8213     directive = ASM_QUAD;
8214 #else
8215   gcc_assert (!TARGET_64BIT);
8216 #endif
8217
8218   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
8219 }
8220
8221 void
8222 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
8223 {
8224   if (TARGET_64BIT)
8225     fprintf (file, "%s%s%d-%s%d\n",
8226              ASM_LONG, LPREFIX, value, LPREFIX, rel);
8227   else if (HAVE_AS_GOTOFF_IN_DATA)
8228     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
8229 #if TARGET_MACHO
8230   else if (TARGET_MACHO)
8231     {
8232       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
8233       machopic_output_function_base_name (file);
8234       fprintf(file, "\n");
8235     }
8236 #endif
8237   else
8238     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
8239                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
8240 }
8241 \f
8242 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
8243    for the target.  */
8244
8245 void
8246 ix86_expand_clear (rtx dest)
8247 {
8248   rtx tmp;
8249
8250   /* We play register width games, which are only valid after reload.  */
8251   gcc_assert (reload_completed);
8252
8253   /* Avoid HImode and its attendant prefix byte.  */
8254   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
8255     dest = gen_rtx_REG (SImode, REGNO (dest));
8256
8257   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
8258
8259   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
8260   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
8261     {
8262       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
8263       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
8264     }
8265
8266   emit_insn (tmp);
8267 }
8268
8269 /* X is an unchanging MEM.  If it is a constant pool reference, return
8270    the constant pool rtx, else NULL.  */
8271
8272 rtx
8273 maybe_get_pool_constant (rtx x)
8274 {
8275   x = ix86_delegitimize_address (XEXP (x, 0));
8276
8277   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
8278     return get_pool_constant (x);
8279
8280   return NULL_RTX;
8281 }
8282
8283 void
8284 ix86_expand_move (enum machine_mode mode, rtx operands[])
8285 {
8286   int strict = (reload_in_progress || reload_completed);
8287   rtx op0, op1;
8288   enum tls_model model;
8289
8290   op0 = operands[0];
8291   op1 = operands[1];
8292
8293   if (GET_CODE (op1) == SYMBOL_REF)
8294     {
8295       model = SYMBOL_REF_TLS_MODEL (op1);
8296       if (model)
8297         {
8298           op1 = legitimize_tls_address (op1, model, true);
8299           op1 = force_operand (op1, op0);
8300           if (op1 == op0)
8301             return;
8302         }
8303     }
8304   else if (GET_CODE (op1) == CONST
8305            && GET_CODE (XEXP (op1, 0)) == PLUS
8306            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
8307     {
8308       model = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (op1, 0), 0));
8309       if (model)
8310         {
8311           rtx addend = XEXP (XEXP (op1, 0), 1);
8312           op1 = legitimize_tls_address (XEXP (XEXP (op1, 0), 0), model, true);
8313           op1 = force_operand (op1, NULL);
8314           op1 = expand_simple_binop (Pmode, PLUS, op1, addend,
8315                                      op0, 1, OPTAB_DIRECT);
8316           if (op1 == op0)
8317             return;
8318         }
8319     }
8320
8321   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
8322     {
8323 #if TARGET_MACHO
8324       if (MACHOPIC_PURE)
8325         {
8326           rtx temp = ((reload_in_progress
8327                        || ((op0 && GET_CODE (op0) == REG)
8328                            && mode == Pmode))
8329                       ? op0 : gen_reg_rtx (Pmode));
8330           op1 = machopic_indirect_data_reference (op1, temp);
8331           op1 = machopic_legitimize_pic_address (op1, mode,
8332                                                  temp == op1 ? 0 : temp);
8333         }
8334       else if (MACHOPIC_INDIRECT)
8335         op1 = machopic_indirect_data_reference (op1, 0);
8336       if (op0 == op1)
8337         return;
8338 #else
8339       if (GET_CODE (op0) == MEM)
8340         op1 = force_reg (Pmode, op1);
8341       else 
8342         op1 = legitimize_address (op1, op1, Pmode);
8343 #endif /* TARGET_MACHO */
8344     }
8345   else
8346     {
8347       if (GET_CODE (op0) == MEM
8348           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
8349               || !push_operand (op0, mode))
8350           && GET_CODE (op1) == MEM)
8351         op1 = force_reg (mode, op1);
8352
8353       if (push_operand (op0, mode)
8354           && ! general_no_elim_operand (op1, mode))
8355         op1 = copy_to_mode_reg (mode, op1);
8356
8357       /* Force large constants in 64bit compilation into register
8358          to get them CSEed.  */
8359       if (TARGET_64BIT && mode == DImode
8360           && immediate_operand (op1, mode)
8361           && !x86_64_zext_immediate_operand (op1, VOIDmode)
8362           && !register_operand (op0, mode)
8363           && optimize && !reload_completed && !reload_in_progress)
8364         op1 = copy_to_mode_reg (mode, op1);
8365
8366       if (FLOAT_MODE_P (mode))
8367         {
8368           /* If we are loading a floating point constant to a register,
8369              force the value to memory now, since we'll get better code
8370              out the back end.  */
8371
8372           if (strict)
8373             ;
8374           else if (GET_CODE (op1) == CONST_DOUBLE)
8375             {
8376               op1 = validize_mem (force_const_mem (mode, op1));
8377               if (!register_operand (op0, mode))
8378                 {
8379                   rtx temp = gen_reg_rtx (mode);
8380                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
8381                   emit_move_insn (op0, temp);
8382                   return;
8383                 }
8384             }
8385         }
8386     }
8387
8388   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
8389 }
8390
8391 void
8392 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
8393 {
8394   rtx op0 = operands[0], op1 = operands[1];
8395
8396   /* Force constants other than zero into memory.  We do not know how
8397      the instructions used to build constants modify the upper 64 bits
8398      of the register, once we have that information we may be able
8399      to handle some of them more efficiently.  */
8400   if ((reload_in_progress | reload_completed) == 0
8401       && register_operand (op0, mode)
8402       && CONSTANT_P (op1) && op1 != CONST0_RTX (mode))
8403     op1 = validize_mem (force_const_mem (mode, op1));
8404
8405   /* Make operand1 a register if it isn't already.  */
8406   if (!no_new_pseudos
8407       && !register_operand (op0, mode)
8408       && !register_operand (op1, mode))
8409     {
8410       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
8411       return;
8412     }
8413
8414   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
8415 }
8416
8417 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go 
8418    straight to ix86_expand_vector_move.  */
8419
8420 void
8421 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
8422 {
8423   rtx op0, op1, m;
8424
8425   op0 = operands[0];
8426   op1 = operands[1];
8427
8428   if (MEM_P (op1))
8429     {
8430       /* If we're optimizing for size, movups is the smallest.  */
8431       if (optimize_size)
8432         {
8433           op0 = gen_lowpart (V4SFmode, op0);
8434           op1 = gen_lowpart (V4SFmode, op1);
8435           emit_insn (gen_sse_movups (op0, op1));
8436           return;
8437         }
8438
8439       /* ??? If we have typed data, then it would appear that using
8440          movdqu is the only way to get unaligned data loaded with
8441          integer type.  */
8442       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
8443         {
8444           op0 = gen_lowpart (V16QImode, op0);
8445           op1 = gen_lowpart (V16QImode, op1);
8446           emit_insn (gen_sse2_movdqu (op0, op1));
8447           return;
8448         }
8449
8450       if (TARGET_SSE2 && mode == V2DFmode)
8451         {
8452           rtx zero;
8453
8454           /* When SSE registers are split into halves, we can avoid
8455              writing to the top half twice.  */
8456           if (TARGET_SSE_SPLIT_REGS)
8457             {
8458               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
8459               zero = op0;
8460             }
8461           else
8462             {
8463               /* ??? Not sure about the best option for the Intel chips.
8464                  The following would seem to satisfy; the register is
8465                  entirely cleared, breaking the dependency chain.  We
8466                  then store to the upper half, with a dependency depth
8467                  of one.  A rumor has it that Intel recommends two movsd
8468                  followed by an unpacklpd, but this is unconfirmed.  And
8469                  given that the dependency depth of the unpacklpd would
8470                  still be one, I'm not sure why this would be better.  */
8471               zero = CONST0_RTX (V2DFmode);
8472             }
8473
8474           m = adjust_address (op1, DFmode, 0);
8475           emit_insn (gen_sse2_loadlpd (op0, zero, m));
8476           m = adjust_address (op1, DFmode, 8);
8477           emit_insn (gen_sse2_loadhpd (op0, op0, m));
8478         }
8479       else
8480         {
8481           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
8482             emit_move_insn (op0, CONST0_RTX (mode));
8483           else
8484             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
8485
8486           if (mode != V4SFmode)
8487             op0 = gen_lowpart (V4SFmode, op0);
8488           m = adjust_address (op1, V2SFmode, 0);
8489           emit_insn (gen_sse_loadlps (op0, op0, m));
8490           m = adjust_address (op1, V2SFmode, 8);
8491           emit_insn (gen_sse_loadhps (op0, op0, m));
8492         }
8493     }
8494   else if (MEM_P (op0))
8495     {
8496       /* If we're optimizing for size, movups is the smallest.  */
8497       if (optimize_size)
8498         {
8499           op0 = gen_lowpart (V4SFmode, op0);
8500           op1 = gen_lowpart (V4SFmode, op1);
8501           emit_insn (gen_sse_movups (op0, op1));
8502           return;
8503         }
8504
8505       /* ??? Similar to above, only less clear because of quote
8506          typeless stores unquote.  */
8507       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
8508           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
8509         {
8510           op0 = gen_lowpart (V16QImode, op0);
8511           op1 = gen_lowpart (V16QImode, op1);
8512           emit_insn (gen_sse2_movdqu (op0, op1));
8513           return;
8514         }
8515
8516       if (TARGET_SSE2 && mode == V2DFmode)
8517         {
8518           m = adjust_address (op0, DFmode, 0);
8519           emit_insn (gen_sse2_storelpd (m, op1));
8520           m = adjust_address (op0, DFmode, 8);
8521           emit_insn (gen_sse2_storehpd (m, op1));
8522         }
8523       else
8524         {
8525           if (mode != V4SFmode)
8526             op1 = gen_lowpart (V4SFmode, op1);
8527           m = adjust_address (op0, V2SFmode, 0);
8528           emit_insn (gen_sse_storelps (m, op1));
8529           m = adjust_address (op0, V2SFmode, 8);
8530           emit_insn (gen_sse_storehps (m, op1));
8531         }
8532     }
8533   else
8534     gcc_unreachable ();
8535 }
8536
8537 /* Expand a push in MODE.  This is some mode for which we do not support
8538    proper push instructions, at least from the registers that we expect
8539    the value to live in.  */
8540
8541 void
8542 ix86_expand_push (enum machine_mode mode, rtx x)
8543 {
8544   rtx tmp;
8545
8546   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
8547                              GEN_INT (-GET_MODE_SIZE (mode)),
8548                              stack_pointer_rtx, 1, OPTAB_DIRECT);
8549   if (tmp != stack_pointer_rtx)
8550     emit_move_insn (stack_pointer_rtx, tmp);
8551
8552   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
8553   emit_move_insn (tmp, x);
8554 }
8555
8556 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
8557    destination to use for the operation.  If different from the true
8558    destination in operands[0], a copy operation will be required.  */
8559
8560 rtx
8561 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
8562                             rtx operands[])
8563 {
8564   int matching_memory;
8565   rtx src1, src2, dst;
8566
8567   dst = operands[0];
8568   src1 = operands[1];
8569   src2 = operands[2];
8570
8571   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
8572   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
8573       && (rtx_equal_p (dst, src2)
8574           || immediate_operand (src1, mode)))
8575     {
8576       rtx temp = src1;
8577       src1 = src2;
8578       src2 = temp;
8579     }
8580
8581   /* If the destination is memory, and we do not have matching source
8582      operands, do things in registers.  */
8583   matching_memory = 0;
8584   if (GET_CODE (dst) == MEM)
8585     {
8586       if (rtx_equal_p (dst, src1))
8587         matching_memory = 1;
8588       else if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
8589                && rtx_equal_p (dst, src2))
8590         matching_memory = 2;
8591       else
8592         dst = gen_reg_rtx (mode);
8593     }
8594
8595   /* Both source operands cannot be in memory.  */
8596   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
8597     {
8598       if (matching_memory != 2)
8599         src2 = force_reg (mode, src2);
8600       else
8601         src1 = force_reg (mode, src1);
8602     }
8603
8604   /* If the operation is not commutable, source 1 cannot be a constant
8605      or non-matching memory.  */
8606   if ((CONSTANT_P (src1)
8607        || (!matching_memory && GET_CODE (src1) == MEM))
8608       && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
8609     src1 = force_reg (mode, src1);
8610
8611   src1 = operands[1] = src1;
8612   src2 = operands[2] = src2;
8613   return dst;
8614 }
8615
8616 /* Similarly, but assume that the destination has already been
8617    set up properly.  */
8618
8619 void
8620 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
8621                                     enum machine_mode mode, rtx operands[])
8622 {
8623   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
8624   gcc_assert (dst == operands[0]);
8625 }
8626
8627 /* Attempt to expand a binary operator.  Make the expansion closer to the
8628    actual machine, then just general_operand, which will allow 3 separate
8629    memory references (one output, two input) in a single insn.  */
8630
8631 void
8632 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
8633                              rtx operands[])
8634 {
8635   rtx src1, src2, dst, op, clob;
8636
8637   dst = ix86_fixup_binary_operands (code, mode, operands);
8638   src1 = operands[1];
8639   src2 = operands[2];
8640
8641  /* Emit the instruction.  */
8642
8643   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
8644   if (reload_in_progress)
8645     {
8646       /* Reload doesn't know about the flags register, and doesn't know that
8647          it doesn't want to clobber it.  We can only do this with PLUS.  */
8648       gcc_assert (code == PLUS);
8649       emit_insn (op);
8650     }
8651   else
8652     {
8653       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8654       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8655     }
8656
8657   /* Fix up the destination if needed.  */
8658   if (dst != operands[0])
8659     emit_move_insn (operands[0], dst);
8660 }
8661
8662 /* Return TRUE or FALSE depending on whether the binary operator meets the
8663    appropriate constraints.  */
8664
8665 int
8666 ix86_binary_operator_ok (enum rtx_code code,
8667                          enum machine_mode mode ATTRIBUTE_UNUSED,
8668                          rtx operands[3])
8669 {
8670   /* Both source operands cannot be in memory.  */
8671   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
8672     return 0;
8673   /* If the operation is not commutable, source 1 cannot be a constant.  */
8674   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
8675     return 0;
8676   /* If the destination is memory, we must have a matching source operand.  */
8677   if (GET_CODE (operands[0]) == MEM
8678       && ! (rtx_equal_p (operands[0], operands[1])
8679             || (GET_RTX_CLASS (code) == RTX_COMM_ARITH
8680                 && rtx_equal_p (operands[0], operands[2]))))
8681     return 0;
8682   /* If the operation is not commutable and the source 1 is memory, we must
8683      have a matching destination.  */
8684   if (GET_CODE (operands[1]) == MEM
8685       && GET_RTX_CLASS (code) != RTX_COMM_ARITH
8686       && ! rtx_equal_p (operands[0], operands[1]))
8687     return 0;
8688   return 1;
8689 }
8690
8691 /* Attempt to expand a unary operator.  Make the expansion closer to the
8692    actual machine, then just general_operand, which will allow 2 separate
8693    memory references (one output, one input) in a single insn.  */
8694
8695 void
8696 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
8697                             rtx operands[])
8698 {
8699   int matching_memory;
8700   rtx src, dst, op, clob;
8701
8702   dst = operands[0];
8703   src = operands[1];
8704
8705   /* If the destination is memory, and we do not have matching source
8706      operands, do things in registers.  */
8707   matching_memory = 0;
8708   if (MEM_P (dst))
8709     {
8710       if (rtx_equal_p (dst, src))
8711         matching_memory = 1;
8712       else
8713         dst = gen_reg_rtx (mode);
8714     }
8715
8716   /* When source operand is memory, destination must match.  */
8717   if (MEM_P (src) && !matching_memory)
8718     src = force_reg (mode, src);
8719
8720   /* Emit the instruction.  */
8721
8722   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
8723   if (reload_in_progress || code == NOT)
8724     {
8725       /* Reload doesn't know about the flags register, and doesn't know that
8726          it doesn't want to clobber it.  */
8727       gcc_assert (code == NOT);
8728       emit_insn (op);
8729     }
8730   else
8731     {
8732       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8733       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8734     }
8735
8736   /* Fix up the destination if needed.  */
8737   if (dst != operands[0])
8738     emit_move_insn (operands[0], dst);
8739 }
8740
8741 /* Return TRUE or FALSE depending on whether the unary operator meets the
8742    appropriate constraints.  */
8743
8744 int
8745 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
8746                         enum machine_mode mode ATTRIBUTE_UNUSED,
8747                         rtx operands[2] ATTRIBUTE_UNUSED)
8748 {
8749   /* If one of operands is memory, source and destination must match.  */
8750   if ((GET_CODE (operands[0]) == MEM
8751        || GET_CODE (operands[1]) == MEM)
8752       && ! rtx_equal_p (operands[0], operands[1]))
8753     return FALSE;
8754   return TRUE;
8755 }
8756
8757 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
8758    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
8759    true, then replicate the mask for all elements of the vector register.
8760    If INVERT is true, then create a mask excluding the sign bit.  */
8761
8762 rtx
8763 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
8764 {
8765   enum machine_mode vec_mode;
8766   HOST_WIDE_INT hi, lo;
8767   int shift = 63;
8768   rtvec v;
8769   rtx mask;
8770
8771   /* Find the sign bit, sign extended to 2*HWI.  */
8772   if (mode == SFmode)
8773     lo = 0x80000000, hi = lo < 0;
8774   else if (HOST_BITS_PER_WIDE_INT >= 64)
8775     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
8776   else
8777     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
8778
8779   if (invert)
8780     lo = ~lo, hi = ~hi;
8781
8782   /* Force this value into the low part of a fp vector constant.  */
8783   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
8784   mask = gen_lowpart (mode, mask);
8785
8786   if (mode == SFmode)
8787     {
8788       if (vect)
8789         v = gen_rtvec (4, mask, mask, mask, mask);
8790       else
8791         v = gen_rtvec (4, mask, CONST0_RTX (SFmode),
8792                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
8793       vec_mode = V4SFmode;
8794     }
8795   else
8796     {
8797       if (vect)
8798         v = gen_rtvec (2, mask, mask);
8799       else
8800         v = gen_rtvec (2, mask, CONST0_RTX (DFmode));
8801       vec_mode = V2DFmode;
8802     }
8803
8804   return force_reg (vec_mode, gen_rtx_CONST_VECTOR (vec_mode, v));
8805 }
8806
8807 /* Generate code for floating point ABS or NEG.  */
8808
8809 void
8810 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
8811                                 rtx operands[])
8812 {
8813   rtx mask, set, use, clob, dst, src;
8814   bool matching_memory;
8815   bool use_sse = false;
8816   bool vector_mode = VECTOR_MODE_P (mode);
8817   enum machine_mode elt_mode = mode;
8818
8819   if (vector_mode)
8820     {
8821       elt_mode = GET_MODE_INNER (mode);
8822       use_sse = true;
8823     }
8824   else if (TARGET_SSE_MATH)
8825     use_sse = SSE_FLOAT_MODE_P (mode);
8826
8827   /* NEG and ABS performed with SSE use bitwise mask operations.
8828      Create the appropriate mask now.  */
8829   if (use_sse)
8830     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
8831   else
8832     {
8833       /* When not using SSE, we don't use the mask, but prefer to keep the
8834          same general form of the insn pattern to reduce duplication when
8835          it comes time to split.  */
8836       mask = const0_rtx;
8837     }
8838
8839   dst = operands[0];
8840   src = operands[1];
8841
8842   /* If the destination is memory, and we don't have matching source
8843      operands, do things in registers.  */
8844   matching_memory = false;
8845   if (MEM_P (dst))
8846     {
8847       if (rtx_equal_p (dst, src))
8848         matching_memory = true;
8849       else
8850         dst = gen_reg_rtx (mode);
8851     }
8852   if (MEM_P (src) && !matching_memory)
8853     src = force_reg (mode, src);
8854
8855   if (vector_mode)
8856     {
8857       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
8858       set = gen_rtx_SET (VOIDmode, dst, set);
8859       emit_insn (set);
8860     }
8861   else
8862     {
8863       set = gen_rtx_fmt_e (code, mode, src);
8864       set = gen_rtx_SET (VOIDmode, dst, set);
8865       use = gen_rtx_USE (VOIDmode, mask);
8866       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8867       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (3, set, use, clob)));
8868     }
8869
8870   if (dst != operands[0])
8871     emit_move_insn (operands[0], dst);
8872 }
8873
8874 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
8875
8876 void
8877 ix86_expand_copysign (rtx operands[])
8878 {
8879   enum machine_mode mode, vmode;
8880   rtx dest, op0, op1, mask, nmask;
8881
8882   dest = operands[0];
8883   op0 = operands[1];
8884   op1 = operands[2];
8885
8886   mode = GET_MODE (dest);
8887   vmode = mode == SFmode ? V4SFmode : V2DFmode;
8888
8889   if (GET_CODE (op0) == CONST_DOUBLE)
8890     {
8891       rtvec v;
8892
8893       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
8894         op0 = simplify_unary_operation (ABS, mode, op0, mode);
8895
8896       if (op0 == CONST0_RTX (mode))
8897         op0 = CONST0_RTX (vmode);
8898       else
8899         {
8900           if (mode == SFmode)
8901             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
8902                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
8903           else
8904             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
8905           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
8906         }
8907
8908       mask = ix86_build_signbit_mask (mode, 0, 0);
8909
8910       if (mode == SFmode)
8911         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
8912       else
8913         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
8914     }
8915   else
8916     {
8917       nmask = ix86_build_signbit_mask (mode, 0, 1);
8918       mask = ix86_build_signbit_mask (mode, 0, 0);
8919
8920       if (mode == SFmode)
8921         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
8922       else
8923         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
8924     }
8925 }
8926
8927 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
8928    be a constant, and so has already been expanded into a vector constant.  */
8929
8930 void
8931 ix86_split_copysign_const (rtx operands[])
8932 {
8933   enum machine_mode mode, vmode;
8934   rtx dest, op0, op1, mask, x;
8935
8936   dest = operands[0];
8937   op0 = operands[1];
8938   op1 = operands[2];
8939   mask = operands[3];
8940
8941   mode = GET_MODE (dest);
8942   vmode = GET_MODE (mask);
8943
8944   dest = simplify_gen_subreg (vmode, dest, mode, 0);
8945   x = gen_rtx_AND (vmode, dest, mask);
8946   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8947
8948   if (op0 != CONST0_RTX (vmode))
8949     {
8950       x = gen_rtx_IOR (vmode, dest, op0);
8951       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8952     }
8953 }
8954
8955 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
8956    so we have to do two masks.  */
8957
8958 void
8959 ix86_split_copysign_var (rtx operands[])
8960 {
8961   enum machine_mode mode, vmode;
8962   rtx dest, scratch, op0, op1, mask, nmask, x;
8963
8964   dest = operands[0];
8965   scratch = operands[1];
8966   op0 = operands[2];
8967   op1 = operands[3];
8968   nmask = operands[4];
8969   mask = operands[5];
8970
8971   mode = GET_MODE (dest);
8972   vmode = GET_MODE (mask);
8973
8974   if (rtx_equal_p (op0, op1))
8975     {
8976       /* Shouldn't happen often (it's useless, obviously), but when it does
8977          we'd generate incorrect code if we continue below.  */
8978       emit_move_insn (dest, op0);
8979       return;
8980     }
8981
8982   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
8983     {
8984       gcc_assert (REGNO (op1) == REGNO (scratch));
8985
8986       x = gen_rtx_AND (vmode, scratch, mask);
8987       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
8988
8989       dest = mask;
8990       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
8991       x = gen_rtx_NOT (vmode, dest);
8992       x = gen_rtx_AND (vmode, x, op0);
8993       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8994     }
8995   else
8996     {
8997       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
8998         {
8999           x = gen_rtx_AND (vmode, scratch, mask);
9000         }
9001       else                                              /* alternative 2,4 */
9002         {
9003           gcc_assert (REGNO (mask) == REGNO (scratch));
9004           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
9005           x = gen_rtx_AND (vmode, scratch, op1);
9006         }
9007       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9008
9009       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
9010         {
9011           dest = simplify_gen_subreg (vmode, op0, mode, 0);
9012           x = gen_rtx_AND (vmode, dest, nmask);
9013         }
9014       else                                              /* alternative 3,4 */
9015         {
9016           gcc_assert (REGNO (nmask) == REGNO (dest));
9017           dest = nmask;
9018           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9019           x = gen_rtx_AND (vmode, dest, op0);
9020         }
9021       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9022     }
9023
9024   x = gen_rtx_IOR (vmode, dest, scratch);
9025   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9026 }
9027
9028 /* Return TRUE or FALSE depending on whether the first SET in INSN
9029    has source and destination with matching CC modes, and that the
9030    CC mode is at least as constrained as REQ_MODE.  */
9031
9032 int
9033 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
9034 {
9035   rtx set;
9036   enum machine_mode set_mode;
9037
9038   set = PATTERN (insn);
9039   if (GET_CODE (set) == PARALLEL)
9040     set = XVECEXP (set, 0, 0);
9041   gcc_assert (GET_CODE (set) == SET);
9042   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
9043
9044   set_mode = GET_MODE (SET_DEST (set));
9045   switch (set_mode)
9046     {
9047     case CCNOmode:
9048       if (req_mode != CCNOmode
9049           && (req_mode != CCmode
9050               || XEXP (SET_SRC (set), 1) != const0_rtx))
9051         return 0;
9052       break;
9053     case CCmode:
9054       if (req_mode == CCGCmode)
9055         return 0;
9056       /* FALLTHRU */
9057     case CCGCmode:
9058       if (req_mode == CCGOCmode || req_mode == CCNOmode)
9059         return 0;
9060       /* FALLTHRU */
9061     case CCGOCmode:
9062       if (req_mode == CCZmode)
9063         return 0;
9064       /* FALLTHRU */
9065     case CCZmode:
9066       break;
9067
9068     default:
9069       gcc_unreachable ();
9070     }
9071
9072   return (GET_MODE (SET_SRC (set)) == set_mode);
9073 }
9074
9075 /* Generate insn patterns to do an integer compare of OPERANDS.  */
9076
9077 static rtx
9078 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
9079 {
9080   enum machine_mode cmpmode;
9081   rtx tmp, flags;
9082
9083   cmpmode = SELECT_CC_MODE (code, op0, op1);
9084   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
9085
9086   /* This is very simple, but making the interface the same as in the
9087      FP case makes the rest of the code easier.  */
9088   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
9089   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
9090
9091   /* Return the test that should be put into the flags user, i.e.
9092      the bcc, scc, or cmov instruction.  */
9093   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
9094 }
9095
9096 /* Figure out whether to use ordered or unordered fp comparisons.
9097    Return the appropriate mode to use.  */
9098
9099 enum machine_mode
9100 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
9101 {
9102   /* ??? In order to make all comparisons reversible, we do all comparisons
9103      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
9104      all forms trapping and nontrapping comparisons, we can make inequality
9105      comparisons trapping again, since it results in better code when using
9106      FCOM based compares.  */
9107   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
9108 }
9109
9110 enum machine_mode
9111 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
9112 {
9113   if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
9114     return ix86_fp_compare_mode (code);
9115   switch (code)
9116     {
9117       /* Only zero flag is needed.  */
9118     case EQ:                    /* ZF=0 */
9119     case NE:                    /* ZF!=0 */
9120       return CCZmode;
9121       /* Codes needing carry flag.  */
9122     case GEU:                   /* CF=0 */
9123     case GTU:                   /* CF=0 & ZF=0 */
9124     case LTU:                   /* CF=1 */
9125     case LEU:                   /* CF=1 | ZF=1 */
9126       return CCmode;
9127       /* Codes possibly doable only with sign flag when
9128          comparing against zero.  */
9129     case GE:                    /* SF=OF   or   SF=0 */
9130     case LT:                    /* SF<>OF  or   SF=1 */
9131       if (op1 == const0_rtx)
9132         return CCGOCmode;
9133       else
9134         /* For other cases Carry flag is not required.  */
9135         return CCGCmode;
9136       /* Codes doable only with sign flag when comparing
9137          against zero, but we miss jump instruction for it
9138          so we need to use relational tests against overflow
9139          that thus needs to be zero.  */
9140     case GT:                    /* ZF=0 & SF=OF */
9141     case LE:                    /* ZF=1 | SF<>OF */
9142       if (op1 == const0_rtx)
9143         return CCNOmode;
9144       else
9145         return CCGCmode;
9146       /* strcmp pattern do (use flags) and combine may ask us for proper
9147          mode.  */
9148     case USE:
9149       return CCmode;
9150     default:
9151       gcc_unreachable ();
9152     }
9153 }
9154
9155 /* Return the fixed registers used for condition codes.  */
9156
9157 static bool
9158 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
9159 {
9160   *p1 = FLAGS_REG;
9161   *p2 = FPSR_REG;
9162   return true;
9163 }
9164
9165 /* If two condition code modes are compatible, return a condition code
9166    mode which is compatible with both.  Otherwise, return
9167    VOIDmode.  */
9168
9169 static enum machine_mode
9170 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
9171 {
9172   if (m1 == m2)
9173     return m1;
9174
9175   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
9176     return VOIDmode;
9177
9178   if ((m1 == CCGCmode && m2 == CCGOCmode)
9179       || (m1 == CCGOCmode && m2 == CCGCmode))
9180     return CCGCmode;
9181
9182   switch (m1)
9183     {
9184     default:
9185       gcc_unreachable ();
9186
9187     case CCmode:
9188     case CCGCmode:
9189     case CCGOCmode:
9190     case CCNOmode:
9191     case CCZmode:
9192       switch (m2)
9193         {
9194         default:
9195           return VOIDmode;
9196
9197         case CCmode:
9198         case CCGCmode:
9199         case CCGOCmode:
9200         case CCNOmode:
9201         case CCZmode:
9202           return CCmode;
9203         }
9204
9205     case CCFPmode:
9206     case CCFPUmode:
9207       /* These are only compatible with themselves, which we already
9208          checked above.  */
9209       return VOIDmode;
9210     }
9211 }
9212
9213 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
9214
9215 int
9216 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
9217 {
9218   enum rtx_code swapped_code = swap_condition (code);
9219   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
9220           || (ix86_fp_comparison_cost (swapped_code)
9221               == ix86_fp_comparison_fcomi_cost (swapped_code)));
9222 }
9223
9224 /* Swap, force into registers, or otherwise massage the two operands
9225    to a fp comparison.  The operands are updated in place; the new
9226    comparison code is returned.  */
9227
9228 static enum rtx_code
9229 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
9230 {
9231   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
9232   rtx op0 = *pop0, op1 = *pop1;
9233   enum machine_mode op_mode = GET_MODE (op0);
9234   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
9235
9236   /* All of the unordered compare instructions only work on registers.
9237      The same is true of the fcomi compare instructions.  The XFmode
9238      compare instructions require registers except when comparing
9239      against zero or when converting operand 1 from fixed point to
9240      floating point.  */
9241
9242   if (!is_sse
9243       && (fpcmp_mode == CCFPUmode
9244           || (op_mode == XFmode
9245               && ! (standard_80387_constant_p (op0) == 1
9246                     || standard_80387_constant_p (op1) == 1)
9247               && GET_CODE (op1) != FLOAT)
9248           || ix86_use_fcomi_compare (code)))
9249     {
9250       op0 = force_reg (op_mode, op0);
9251       op1 = force_reg (op_mode, op1);
9252     }
9253   else
9254     {
9255       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
9256          things around if they appear profitable, otherwise force op0
9257          into a register.  */
9258
9259       if (standard_80387_constant_p (op0) == 0
9260           || (GET_CODE (op0) == MEM
9261               && ! (standard_80387_constant_p (op1) == 0
9262                     || GET_CODE (op1) == MEM)))
9263         {
9264           rtx tmp;
9265           tmp = op0, op0 = op1, op1 = tmp;
9266           code = swap_condition (code);
9267         }
9268
9269       if (GET_CODE (op0) != REG)
9270         op0 = force_reg (op_mode, op0);
9271
9272       if (CONSTANT_P (op1))
9273         {
9274           int tmp = standard_80387_constant_p (op1);
9275           if (tmp == 0)
9276             op1 = validize_mem (force_const_mem (op_mode, op1));
9277           else if (tmp == 1)
9278             {
9279               if (TARGET_CMOVE)
9280                 op1 = force_reg (op_mode, op1);
9281             }
9282           else
9283             op1 = force_reg (op_mode, op1);
9284         }
9285     }
9286
9287   /* Try to rearrange the comparison to make it cheaper.  */
9288   if (ix86_fp_comparison_cost (code)
9289       > ix86_fp_comparison_cost (swap_condition (code))
9290       && (GET_CODE (op1) == REG || !no_new_pseudos))
9291     {
9292       rtx tmp;
9293       tmp = op0, op0 = op1, op1 = tmp;
9294       code = swap_condition (code);
9295       if (GET_CODE (op0) != REG)
9296         op0 = force_reg (op_mode, op0);
9297     }
9298
9299   *pop0 = op0;
9300   *pop1 = op1;
9301   return code;
9302 }
9303
9304 /* Convert comparison codes we use to represent FP comparison to integer
9305    code that will result in proper branch.  Return UNKNOWN if no such code
9306    is available.  */
9307
9308 enum rtx_code
9309 ix86_fp_compare_code_to_integer (enum rtx_code code)
9310 {
9311   switch (code)
9312     {
9313     case GT:
9314       return GTU;
9315     case GE:
9316       return GEU;
9317     case ORDERED:
9318     case UNORDERED:
9319       return code;
9320       break;
9321     case UNEQ:
9322       return EQ;
9323       break;
9324     case UNLT:
9325       return LTU;
9326       break;
9327     case UNLE:
9328       return LEU;
9329       break;
9330     case LTGT:
9331       return NE;
9332       break;
9333     default:
9334       return UNKNOWN;
9335     }
9336 }
9337
9338 /* Split comparison code CODE into comparisons we can do using branch
9339    instructions.  BYPASS_CODE is comparison code for branch that will
9340    branch around FIRST_CODE and SECOND_CODE.  If some of branches
9341    is not required, set value to UNKNOWN.
9342    We never require more than two branches.  */
9343
9344 void
9345 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
9346                           enum rtx_code *first_code,
9347                           enum rtx_code *second_code)
9348 {
9349   *first_code = code;
9350   *bypass_code = UNKNOWN;
9351   *second_code = UNKNOWN;
9352
9353   /* The fcomi comparison sets flags as follows:
9354
9355      cmp    ZF PF CF
9356      >      0  0  0
9357      <      0  0  1
9358      =      1  0  0
9359      un     1  1  1 */
9360
9361   switch (code)
9362     {
9363     case GT:                    /* GTU - CF=0 & ZF=0 */
9364     case GE:                    /* GEU - CF=0 */
9365     case ORDERED:               /* PF=0 */
9366     case UNORDERED:             /* PF=1 */
9367     case UNEQ:                  /* EQ - ZF=1 */
9368     case UNLT:                  /* LTU - CF=1 */
9369     case UNLE:                  /* LEU - CF=1 | ZF=1 */
9370     case LTGT:                  /* EQ - ZF=0 */
9371       break;
9372     case LT:                    /* LTU - CF=1 - fails on unordered */
9373       *first_code = UNLT;
9374       *bypass_code = UNORDERED;
9375       break;
9376     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
9377       *first_code = UNLE;
9378       *bypass_code = UNORDERED;
9379       break;
9380     case EQ:                    /* EQ - ZF=1 - fails on unordered */
9381       *first_code = UNEQ;
9382       *bypass_code = UNORDERED;
9383       break;
9384     case NE:                    /* NE - ZF=0 - fails on unordered */
9385       *first_code = LTGT;
9386       *second_code = UNORDERED;
9387       break;
9388     case UNGE:                  /* GEU - CF=0 - fails on unordered */
9389       *first_code = GE;
9390       *second_code = UNORDERED;
9391       break;
9392     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
9393       *first_code = GT;
9394       *second_code = UNORDERED;
9395       break;
9396     default:
9397       gcc_unreachable ();
9398     }
9399   if (!TARGET_IEEE_FP)
9400     {
9401       *second_code = UNKNOWN;
9402       *bypass_code = UNKNOWN;
9403     }
9404 }
9405
9406 /* Return cost of comparison done fcom + arithmetics operations on AX.
9407    All following functions do use number of instructions as a cost metrics.
9408    In future this should be tweaked to compute bytes for optimize_size and
9409    take into account performance of various instructions on various CPUs.  */
9410 static int
9411 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
9412 {
9413   if (!TARGET_IEEE_FP)
9414     return 4;
9415   /* The cost of code output by ix86_expand_fp_compare.  */
9416   switch (code)
9417     {
9418     case UNLE:
9419     case UNLT:
9420     case LTGT:
9421     case GT:
9422     case GE:
9423     case UNORDERED:
9424     case ORDERED:
9425     case UNEQ:
9426       return 4;
9427       break;
9428     case LT:
9429     case NE:
9430     case EQ:
9431     case UNGE:
9432       return 5;
9433       break;
9434     case LE:
9435     case UNGT:
9436       return 6;
9437       break;
9438     default:
9439       gcc_unreachable ();
9440     }
9441 }
9442
9443 /* Return cost of comparison done using fcomi operation.
9444    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
9445 static int
9446 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
9447 {
9448   enum rtx_code bypass_code, first_code, second_code;
9449   /* Return arbitrarily high cost when instruction is not supported - this
9450      prevents gcc from using it.  */
9451   if (!TARGET_CMOVE)
9452     return 1024;
9453   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9454   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
9455 }
9456
9457 /* Return cost of comparison done using sahf operation.
9458    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
9459 static int
9460 ix86_fp_comparison_sahf_cost (enum rtx_code code)
9461 {
9462   enum rtx_code bypass_code, first_code, second_code;
9463   /* Return arbitrarily high cost when instruction is not preferred - this
9464      avoids gcc from using it.  */
9465   if (!TARGET_USE_SAHF && !optimize_size)
9466     return 1024;
9467   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9468   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
9469 }
9470
9471 /* Compute cost of the comparison done using any method.
9472    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
9473 static int
9474 ix86_fp_comparison_cost (enum rtx_code code)
9475 {
9476   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
9477   int min;
9478
9479   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
9480   sahf_cost = ix86_fp_comparison_sahf_cost (code);
9481
9482   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
9483   if (min > sahf_cost)
9484     min = sahf_cost;
9485   if (min > fcomi_cost)
9486     min = fcomi_cost;
9487   return min;
9488 }
9489
9490 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
9491
9492 static rtx
9493 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
9494                         rtx *second_test, rtx *bypass_test)
9495 {
9496   enum machine_mode fpcmp_mode, intcmp_mode;
9497   rtx tmp, tmp2;
9498   int cost = ix86_fp_comparison_cost (code);
9499   enum rtx_code bypass_code, first_code, second_code;
9500
9501   fpcmp_mode = ix86_fp_compare_mode (code);
9502   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
9503
9504   if (second_test)
9505     *second_test = NULL_RTX;
9506   if (bypass_test)
9507     *bypass_test = NULL_RTX;
9508
9509   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9510
9511   /* Do fcomi/sahf based test when profitable.  */
9512   if ((bypass_code == UNKNOWN || bypass_test)
9513       && (second_code == UNKNOWN || second_test)
9514       && ix86_fp_comparison_arithmetics_cost (code) > cost)
9515     {
9516       if (TARGET_CMOVE)
9517         {
9518           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9519           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
9520                              tmp);
9521           emit_insn (tmp);
9522         }
9523       else
9524         {
9525           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9526           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
9527           if (!scratch)
9528             scratch = gen_reg_rtx (HImode);
9529           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
9530           emit_insn (gen_x86_sahf_1 (scratch));
9531         }
9532
9533       /* The FP codes work out to act like unsigned.  */
9534       intcmp_mode = fpcmp_mode;
9535       code = first_code;
9536       if (bypass_code != UNKNOWN)
9537         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
9538                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
9539                                        const0_rtx);
9540       if (second_code != UNKNOWN)
9541         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
9542                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
9543                                        const0_rtx);
9544     }
9545   else
9546     {
9547       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
9548       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9549       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
9550       if (!scratch)
9551         scratch = gen_reg_rtx (HImode);
9552       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
9553
9554       /* In the unordered case, we have to check C2 for NaN's, which
9555          doesn't happen to work out to anything nice combination-wise.
9556          So do some bit twiddling on the value we've got in AH to come
9557          up with an appropriate set of condition codes.  */
9558
9559       intcmp_mode = CCNOmode;
9560       switch (code)
9561         {
9562         case GT:
9563         case UNGT:
9564           if (code == GT || !TARGET_IEEE_FP)
9565             {
9566               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9567               code = EQ;
9568             }
9569           else
9570             {
9571               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9572               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9573               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
9574               intcmp_mode = CCmode;
9575               code = GEU;
9576             }
9577           break;
9578         case LT:
9579         case UNLT:
9580           if (code == LT && TARGET_IEEE_FP)
9581             {
9582               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9583               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
9584               intcmp_mode = CCmode;
9585               code = EQ;
9586             }
9587           else
9588             {
9589               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
9590               code = NE;
9591             }
9592           break;
9593         case GE:
9594         case UNGE:
9595           if (code == GE || !TARGET_IEEE_FP)
9596             {
9597               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
9598               code = EQ;
9599             }
9600           else
9601             {
9602               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9603               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
9604                                              GEN_INT (0x01)));
9605               code = NE;
9606             }
9607           break;
9608         case LE:
9609         case UNLE:
9610           if (code == LE && TARGET_IEEE_FP)
9611             {
9612               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9613               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9614               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
9615               intcmp_mode = CCmode;
9616               code = LTU;
9617             }
9618           else
9619             {
9620               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9621               code = NE;
9622             }
9623           break;
9624         case EQ:
9625         case UNEQ:
9626           if (code == EQ && TARGET_IEEE_FP)
9627             {
9628               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9629               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
9630               intcmp_mode = CCmode;
9631               code = EQ;
9632             }
9633           else
9634             {
9635               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
9636               code = NE;
9637               break;
9638             }
9639           break;
9640         case NE:
9641         case LTGT:
9642           if (code == NE && TARGET_IEEE_FP)
9643             {
9644               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9645               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
9646                                              GEN_INT (0x40)));
9647               code = NE;
9648             }
9649           else
9650             {
9651               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
9652               code = EQ;
9653             }
9654           break;
9655
9656         case UNORDERED:
9657           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
9658           code = NE;
9659           break;
9660         case ORDERED:
9661           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
9662           code = EQ;
9663           break;
9664
9665         default:
9666           gcc_unreachable ();
9667         }
9668     }
9669
9670   /* Return the test that should be put into the flags user, i.e.
9671      the bcc, scc, or cmov instruction.  */
9672   return gen_rtx_fmt_ee (code, VOIDmode,
9673                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
9674                          const0_rtx);
9675 }
9676
9677 rtx
9678 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
9679 {
9680   rtx op0, op1, ret;
9681   op0 = ix86_compare_op0;
9682   op1 = ix86_compare_op1;
9683
9684   if (second_test)
9685     *second_test = NULL_RTX;
9686   if (bypass_test)
9687     *bypass_test = NULL_RTX;
9688
9689   if (ix86_compare_emitted)
9690     {
9691       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
9692       ix86_compare_emitted = NULL_RTX;
9693     }
9694   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
9695     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
9696                                   second_test, bypass_test);
9697   else
9698     ret = ix86_expand_int_compare (code, op0, op1);
9699
9700   return ret;
9701 }
9702
9703 /* Return true if the CODE will result in nontrivial jump sequence.  */
9704 bool
9705 ix86_fp_jump_nontrivial_p (enum rtx_code code)
9706 {
9707   enum rtx_code bypass_code, first_code, second_code;
9708   if (!TARGET_CMOVE)
9709     return true;
9710   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9711   return bypass_code != UNKNOWN || second_code != UNKNOWN;
9712 }
9713
9714 void
9715 ix86_expand_branch (enum rtx_code code, rtx label)
9716 {
9717   rtx tmp;
9718
9719   switch (GET_MODE (ix86_compare_op0))
9720     {
9721     case QImode:
9722     case HImode:
9723     case SImode:
9724       simple:
9725       tmp = ix86_expand_compare (code, NULL, NULL);
9726       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9727                                   gen_rtx_LABEL_REF (VOIDmode, label),
9728                                   pc_rtx);
9729       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
9730       return;
9731
9732     case SFmode:
9733     case DFmode:
9734     case XFmode:
9735       {
9736         rtvec vec;
9737         int use_fcomi;
9738         enum rtx_code bypass_code, first_code, second_code;
9739
9740         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
9741                                              &ix86_compare_op1);
9742
9743         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9744
9745         /* Check whether we will use the natural sequence with one jump.  If
9746            so, we can expand jump early.  Otherwise delay expansion by
9747            creating compound insn to not confuse optimizers.  */
9748         if (bypass_code == UNKNOWN && second_code == UNKNOWN
9749             && TARGET_CMOVE)
9750           {
9751             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
9752                                   gen_rtx_LABEL_REF (VOIDmode, label),
9753                                   pc_rtx, NULL_RTX, NULL_RTX);
9754           }
9755         else
9756           {
9757             tmp = gen_rtx_fmt_ee (code, VOIDmode,
9758                                   ix86_compare_op0, ix86_compare_op1);
9759             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9760                                         gen_rtx_LABEL_REF (VOIDmode, label),
9761                                         pc_rtx);
9762             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
9763
9764             use_fcomi = ix86_use_fcomi_compare (code);
9765             vec = rtvec_alloc (3 + !use_fcomi);
9766             RTVEC_ELT (vec, 0) = tmp;
9767             RTVEC_ELT (vec, 1)
9768               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
9769             RTVEC_ELT (vec, 2)
9770               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
9771             if (! use_fcomi)
9772               RTVEC_ELT (vec, 3)
9773                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
9774
9775             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
9776           }
9777         return;
9778       }
9779
9780     case DImode:
9781       if (TARGET_64BIT)
9782         goto simple;
9783     case TImode:
9784       /* Expand DImode branch into multiple compare+branch.  */
9785       {
9786         rtx lo[2], hi[2], label2;
9787         enum rtx_code code1, code2, code3;
9788         enum machine_mode submode;
9789
9790         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
9791           {
9792             tmp = ix86_compare_op0;
9793             ix86_compare_op0 = ix86_compare_op1;
9794             ix86_compare_op1 = tmp;
9795             code = swap_condition (code);
9796           }
9797         if (GET_MODE (ix86_compare_op0) == DImode)
9798           {
9799             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
9800             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
9801             submode = SImode;
9802           }
9803         else
9804           {
9805             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
9806             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
9807             submode = DImode;
9808           }
9809
9810         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
9811            avoid two branches.  This costs one extra insn, so disable when
9812            optimizing for size.  */
9813
9814         if ((code == EQ || code == NE)
9815             && (!optimize_size
9816                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
9817           {
9818             rtx xor0, xor1;
9819
9820             xor1 = hi[0];
9821             if (hi[1] != const0_rtx)
9822               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
9823                                    NULL_RTX, 0, OPTAB_WIDEN);
9824
9825             xor0 = lo[0];
9826             if (lo[1] != const0_rtx)
9827               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
9828                                    NULL_RTX, 0, OPTAB_WIDEN);
9829
9830             tmp = expand_binop (submode, ior_optab, xor1, xor0,
9831                                 NULL_RTX, 0, OPTAB_WIDEN);
9832
9833             ix86_compare_op0 = tmp;
9834             ix86_compare_op1 = const0_rtx;
9835             ix86_expand_branch (code, label);
9836             return;
9837           }
9838
9839         /* Otherwise, if we are doing less-than or greater-or-equal-than,
9840            op1 is a constant and the low word is zero, then we can just
9841            examine the high word.  */
9842
9843         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
9844           switch (code)
9845             {
9846             case LT: case LTU: case GE: case GEU:
9847               ix86_compare_op0 = hi[0];
9848               ix86_compare_op1 = hi[1];
9849               ix86_expand_branch (code, label);
9850               return;
9851             default:
9852               break;
9853             }
9854
9855         /* Otherwise, we need two or three jumps.  */
9856
9857         label2 = gen_label_rtx ();
9858
9859         code1 = code;
9860         code2 = swap_condition (code);
9861         code3 = unsigned_condition (code);
9862
9863         switch (code)
9864           {
9865           case LT: case GT: case LTU: case GTU:
9866             break;
9867
9868           case LE:   code1 = LT;  code2 = GT;  break;
9869           case GE:   code1 = GT;  code2 = LT;  break;
9870           case LEU:  code1 = LTU; code2 = GTU; break;
9871           case GEU:  code1 = GTU; code2 = LTU; break;
9872
9873           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
9874           case NE:   code2 = UNKNOWN; break;
9875
9876           default:
9877             gcc_unreachable ();
9878           }
9879
9880         /*
9881          * a < b =>
9882          *    if (hi(a) < hi(b)) goto true;
9883          *    if (hi(a) > hi(b)) goto false;
9884          *    if (lo(a) < lo(b)) goto true;
9885          *  false:
9886          */
9887
9888         ix86_compare_op0 = hi[0];
9889         ix86_compare_op1 = hi[1];
9890
9891         if (code1 != UNKNOWN)
9892           ix86_expand_branch (code1, label);
9893         if (code2 != UNKNOWN)
9894           ix86_expand_branch (code2, label2);
9895
9896         ix86_compare_op0 = lo[0];
9897         ix86_compare_op1 = lo[1];
9898         ix86_expand_branch (code3, label);
9899
9900         if (code2 != UNKNOWN)
9901           emit_label (label2);
9902         return;
9903       }
9904
9905     default:
9906       gcc_unreachable ();
9907     }
9908 }
9909
9910 /* Split branch based on floating point condition.  */
9911 void
9912 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
9913                       rtx target1, rtx target2, rtx tmp, rtx pushed)
9914 {
9915   rtx second, bypass;
9916   rtx label = NULL_RTX;
9917   rtx condition;
9918   int bypass_probability = -1, second_probability = -1, probability = -1;
9919   rtx i;
9920
9921   if (target2 != pc_rtx)
9922     {
9923       rtx tmp = target2;
9924       code = reverse_condition_maybe_unordered (code);
9925       target2 = target1;
9926       target1 = tmp;
9927     }
9928
9929   condition = ix86_expand_fp_compare (code, op1, op2,
9930                                       tmp, &second, &bypass);
9931
9932   /* Remove pushed operand from stack.  */
9933   if (pushed)
9934     ix86_free_from_memory (GET_MODE (pushed));
9935
9936   if (split_branch_probability >= 0)
9937     {
9938       /* Distribute the probabilities across the jumps.
9939          Assume the BYPASS and SECOND to be always test
9940          for UNORDERED.  */
9941       probability = split_branch_probability;
9942
9943       /* Value of 1 is low enough to make no need for probability
9944          to be updated.  Later we may run some experiments and see
9945          if unordered values are more frequent in practice.  */
9946       if (bypass)
9947         bypass_probability = 1;
9948       if (second)
9949         second_probability = 1;
9950     }
9951   if (bypass != NULL_RTX)
9952     {
9953       label = gen_label_rtx ();
9954       i = emit_jump_insn (gen_rtx_SET
9955                           (VOIDmode, pc_rtx,
9956                            gen_rtx_IF_THEN_ELSE (VOIDmode,
9957                                                  bypass,
9958                                                  gen_rtx_LABEL_REF (VOIDmode,
9959                                                                     label),
9960                                                  pc_rtx)));
9961       if (bypass_probability >= 0)
9962         REG_NOTES (i)
9963           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9964                                GEN_INT (bypass_probability),
9965                                REG_NOTES (i));
9966     }
9967   i = emit_jump_insn (gen_rtx_SET
9968                       (VOIDmode, pc_rtx,
9969                        gen_rtx_IF_THEN_ELSE (VOIDmode,
9970                                              condition, target1, target2)));
9971   if (probability >= 0)
9972     REG_NOTES (i)
9973       = gen_rtx_EXPR_LIST (REG_BR_PROB,
9974                            GEN_INT (probability),
9975                            REG_NOTES (i));
9976   if (second != NULL_RTX)
9977     {
9978       i = emit_jump_insn (gen_rtx_SET
9979                           (VOIDmode, pc_rtx,
9980                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
9981                                                  target2)));
9982       if (second_probability >= 0)
9983         REG_NOTES (i)
9984           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9985                                GEN_INT (second_probability),
9986                                REG_NOTES (i));
9987     }
9988   if (label != NULL_RTX)
9989     emit_label (label);
9990 }
9991
9992 int
9993 ix86_expand_setcc (enum rtx_code code, rtx dest)
9994 {
9995   rtx ret, tmp, tmpreg, equiv;
9996   rtx second_test, bypass_test;
9997
9998   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
9999     return 0; /* FAIL */
10000
10001   gcc_assert (GET_MODE (dest) == QImode);
10002
10003   ret = ix86_expand_compare (code, &second_test, &bypass_test);
10004   PUT_MODE (ret, QImode);
10005
10006   tmp = dest;
10007   tmpreg = dest;
10008
10009   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
10010   if (bypass_test || second_test)
10011     {
10012       rtx test = second_test;
10013       int bypass = 0;
10014       rtx tmp2 = gen_reg_rtx (QImode);
10015       if (bypass_test)
10016         {
10017           gcc_assert (!second_test);
10018           test = bypass_test;
10019           bypass = 1;
10020           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
10021         }
10022       PUT_MODE (test, QImode);
10023       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
10024
10025       if (bypass)
10026         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
10027       else
10028         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
10029     }
10030
10031   /* Attach a REG_EQUAL note describing the comparison result.  */
10032   if (ix86_compare_op0 && ix86_compare_op1)
10033     {
10034       equiv = simplify_gen_relational (code, QImode,
10035                                        GET_MODE (ix86_compare_op0),
10036                                        ix86_compare_op0, ix86_compare_op1);
10037       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
10038     }
10039
10040   return 1; /* DONE */
10041 }
10042
10043 /* Expand comparison setting or clearing carry flag.  Return true when
10044    successful and set pop for the operation.  */
10045 static bool
10046 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
10047 {
10048   enum machine_mode mode =
10049     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
10050
10051   /* Do not handle DImode compares that go trought special path.  Also we can't
10052      deal with FP compares yet.  This is possible to add.  */
10053   if (mode == (TARGET_64BIT ? TImode : DImode))
10054     return false;
10055   if (FLOAT_MODE_P (mode))
10056     {
10057       rtx second_test = NULL, bypass_test = NULL;
10058       rtx compare_op, compare_seq;
10059
10060       /* Shortcut:  following common codes never translate into carry flag compares.  */
10061       if (code == EQ || code == NE || code == UNEQ || code == LTGT
10062           || code == ORDERED || code == UNORDERED)
10063         return false;
10064
10065       /* These comparisons require zero flag; swap operands so they won't.  */
10066       if ((code == GT || code == UNLE || code == LE || code == UNGT)
10067           && !TARGET_IEEE_FP)
10068         {
10069           rtx tmp = op0;
10070           op0 = op1;
10071           op1 = tmp;
10072           code = swap_condition (code);
10073         }
10074
10075       /* Try to expand the comparison and verify that we end up with carry flag
10076          based comparison.  This is fails to be true only when we decide to expand
10077          comparison using arithmetic that is not too common scenario.  */
10078       start_sequence ();
10079       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10080                                            &second_test, &bypass_test);
10081       compare_seq = get_insns ();
10082       end_sequence ();
10083
10084       if (second_test || bypass_test)
10085         return false;
10086       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10087           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10088         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
10089       else
10090         code = GET_CODE (compare_op);
10091       if (code != LTU && code != GEU)
10092         return false;
10093       emit_insn (compare_seq);
10094       *pop = compare_op;
10095       return true;
10096     }
10097   if (!INTEGRAL_MODE_P (mode))
10098     return false;
10099   switch (code)
10100     {
10101     case LTU:
10102     case GEU:
10103       break;
10104
10105     /* Convert a==0 into (unsigned)a<1.  */
10106     case EQ:
10107     case NE:
10108       if (op1 != const0_rtx)
10109         return false;
10110       op1 = const1_rtx;
10111       code = (code == EQ ? LTU : GEU);
10112       break;
10113
10114     /* Convert a>b into b<a or a>=b-1.  */
10115     case GTU:
10116     case LEU:
10117       if (GET_CODE (op1) == CONST_INT)
10118         {
10119           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
10120           /* Bail out on overflow.  We still can swap operands but that
10121              would force loading of the constant into register.  */
10122           if (op1 == const0_rtx
10123               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
10124             return false;
10125           code = (code == GTU ? GEU : LTU);
10126         }
10127       else
10128         {
10129           rtx tmp = op1;
10130           op1 = op0;
10131           op0 = tmp;
10132           code = (code == GTU ? LTU : GEU);
10133         }
10134       break;
10135
10136     /* Convert a>=0 into (unsigned)a<0x80000000.  */
10137     case LT:
10138     case GE:
10139       if (mode == DImode || op1 != const0_rtx)
10140         return false;
10141       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10142       code = (code == LT ? GEU : LTU);
10143       break;
10144     case LE:
10145     case GT:
10146       if (mode == DImode || op1 != constm1_rtx)
10147         return false;
10148       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10149       code = (code == LE ? GEU : LTU);
10150       break;
10151
10152     default:
10153       return false;
10154     }
10155   /* Swapping operands may cause constant to appear as first operand.  */
10156   if (!nonimmediate_operand (op0, VOIDmode))
10157     {
10158       if (no_new_pseudos)
10159         return false;
10160       op0 = force_reg (mode, op0);
10161     }
10162   ix86_compare_op0 = op0;
10163   ix86_compare_op1 = op1;
10164   *pop = ix86_expand_compare (code, NULL, NULL);
10165   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
10166   return true;
10167 }
10168
10169 int
10170 ix86_expand_int_movcc (rtx operands[])
10171 {
10172   enum rtx_code code = GET_CODE (operands[1]), compare_code;
10173   rtx compare_seq, compare_op;
10174   rtx second_test, bypass_test;
10175   enum machine_mode mode = GET_MODE (operands[0]);
10176   bool sign_bit_compare_p = false;;
10177
10178   start_sequence ();
10179   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
10180   compare_seq = get_insns ();
10181   end_sequence ();
10182
10183   compare_code = GET_CODE (compare_op);
10184
10185   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
10186       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
10187     sign_bit_compare_p = true;
10188
10189   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
10190      HImode insns, we'd be swallowed in word prefix ops.  */
10191
10192   if ((mode != HImode || TARGET_FAST_PREFIX)
10193       && (mode != (TARGET_64BIT ? TImode : DImode))
10194       && GET_CODE (operands[2]) == CONST_INT
10195       && GET_CODE (operands[3]) == CONST_INT)
10196     {
10197       rtx out = operands[0];
10198       HOST_WIDE_INT ct = INTVAL (operands[2]);
10199       HOST_WIDE_INT cf = INTVAL (operands[3]);
10200       HOST_WIDE_INT diff;
10201
10202       diff = ct - cf;
10203       /*  Sign bit compares are better done using shifts than we do by using
10204           sbb.  */
10205       if (sign_bit_compare_p
10206           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
10207                                              ix86_compare_op1, &compare_op))
10208         {
10209           /* Detect overlap between destination and compare sources.  */
10210           rtx tmp = out;
10211
10212           if (!sign_bit_compare_p)
10213             {
10214               bool fpcmp = false;
10215
10216               compare_code = GET_CODE (compare_op);
10217
10218               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10219                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10220                 {
10221                   fpcmp = true;
10222                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
10223                 }
10224
10225               /* To simplify rest of code, restrict to the GEU case.  */
10226               if (compare_code == LTU)
10227                 {
10228                   HOST_WIDE_INT tmp = ct;
10229                   ct = cf;
10230                   cf = tmp;
10231                   compare_code = reverse_condition (compare_code);
10232                   code = reverse_condition (code);
10233                 }
10234               else
10235                 {
10236                   if (fpcmp)
10237                     PUT_CODE (compare_op,
10238                               reverse_condition_maybe_unordered
10239                                 (GET_CODE (compare_op)));
10240                   else
10241                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
10242                 }
10243               diff = ct - cf;
10244
10245               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
10246                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
10247                 tmp = gen_reg_rtx (mode);
10248
10249               if (mode == DImode)
10250                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
10251               else
10252                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
10253             }
10254           else
10255             {
10256               if (code == GT || code == GE)
10257                 code = reverse_condition (code);
10258               else
10259                 {
10260                   HOST_WIDE_INT tmp = ct;
10261                   ct = cf;
10262                   cf = tmp;
10263                   diff = ct - cf;
10264                 }
10265               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
10266                                      ix86_compare_op1, VOIDmode, 0, -1);
10267             }
10268
10269           if (diff == 1)
10270             {
10271               /*
10272                * cmpl op0,op1
10273                * sbbl dest,dest
10274                * [addl dest, ct]
10275                *
10276                * Size 5 - 8.
10277                */
10278               if (ct)
10279                 tmp = expand_simple_binop (mode, PLUS,
10280                                            tmp, GEN_INT (ct),
10281                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10282             }
10283           else if (cf == -1)
10284             {
10285               /*
10286                * cmpl op0,op1
10287                * sbbl dest,dest
10288                * orl $ct, dest
10289                *
10290                * Size 8.
10291                */
10292               tmp = expand_simple_binop (mode, IOR,
10293                                          tmp, GEN_INT (ct),
10294                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10295             }
10296           else if (diff == -1 && ct)
10297             {
10298               /*
10299                * cmpl op0,op1
10300                * sbbl dest,dest
10301                * notl dest
10302                * [addl dest, cf]
10303                *
10304                * Size 8 - 11.
10305                */
10306               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10307               if (cf)
10308                 tmp = expand_simple_binop (mode, PLUS,
10309                                            copy_rtx (tmp), GEN_INT (cf),
10310                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10311             }
10312           else
10313             {
10314               /*
10315                * cmpl op0,op1
10316                * sbbl dest,dest
10317                * [notl dest]
10318                * andl cf - ct, dest
10319                * [addl dest, ct]
10320                *
10321                * Size 8 - 11.
10322                */
10323
10324               if (cf == 0)
10325                 {
10326                   cf = ct;
10327                   ct = 0;
10328                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10329                 }
10330
10331               tmp = expand_simple_binop (mode, AND,
10332                                          copy_rtx (tmp),
10333                                          gen_int_mode (cf - ct, mode),
10334                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10335               if (ct)
10336                 tmp = expand_simple_binop (mode, PLUS,
10337                                            copy_rtx (tmp), GEN_INT (ct),
10338                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10339             }
10340
10341           if (!rtx_equal_p (tmp, out))
10342             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
10343
10344           return 1; /* DONE */
10345         }
10346
10347       if (diff < 0)
10348         {
10349           HOST_WIDE_INT tmp;
10350           tmp = ct, ct = cf, cf = tmp;
10351           diff = -diff;
10352           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
10353             {
10354               /* We may be reversing unordered compare to normal compare, that
10355                  is not valid in general (we may convert non-trapping condition
10356                  to trapping one), however on i386 we currently emit all
10357                  comparisons unordered.  */
10358               compare_code = reverse_condition_maybe_unordered (compare_code);
10359               code = reverse_condition_maybe_unordered (code);
10360             }
10361           else
10362             {
10363               compare_code = reverse_condition (compare_code);
10364               code = reverse_condition (code);
10365             }
10366         }
10367
10368       compare_code = UNKNOWN;
10369       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
10370           && GET_CODE (ix86_compare_op1) == CONST_INT)
10371         {
10372           if (ix86_compare_op1 == const0_rtx
10373               && (code == LT || code == GE))
10374             compare_code = code;
10375           else if (ix86_compare_op1 == constm1_rtx)
10376             {
10377               if (code == LE)
10378                 compare_code = LT;
10379               else if (code == GT)
10380                 compare_code = GE;
10381             }
10382         }
10383
10384       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
10385       if (compare_code != UNKNOWN
10386           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
10387           && (cf == -1 || ct == -1))
10388         {
10389           /* If lea code below could be used, only optimize
10390              if it results in a 2 insn sequence.  */
10391
10392           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
10393                  || diff == 3 || diff == 5 || diff == 9)
10394               || (compare_code == LT && ct == -1)
10395               || (compare_code == GE && cf == -1))
10396             {
10397               /*
10398                * notl op1       (if necessary)
10399                * sarl $31, op1
10400                * orl cf, op1
10401                */
10402               if (ct != -1)
10403                 {
10404                   cf = ct;
10405                   ct = -1;
10406                   code = reverse_condition (code);
10407                 }
10408
10409               out = emit_store_flag (out, code, ix86_compare_op0,
10410                                      ix86_compare_op1, VOIDmode, 0, -1);
10411
10412               out = expand_simple_binop (mode, IOR,
10413                                          out, GEN_INT (cf),
10414                                          out, 1, OPTAB_DIRECT);
10415               if (out != operands[0])
10416                 emit_move_insn (operands[0], out);
10417
10418               return 1; /* DONE */
10419             }
10420         }
10421
10422
10423       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
10424            || diff == 3 || diff == 5 || diff == 9)
10425           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
10426           && (mode != DImode
10427               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
10428         {
10429           /*
10430            * xorl dest,dest
10431            * cmpl op1,op2
10432            * setcc dest
10433            * lea cf(dest*(ct-cf)),dest
10434            *
10435            * Size 14.
10436            *
10437            * This also catches the degenerate setcc-only case.
10438            */
10439
10440           rtx tmp;
10441           int nops;
10442
10443           out = emit_store_flag (out, code, ix86_compare_op0,
10444                                  ix86_compare_op1, VOIDmode, 0, 1);
10445
10446           nops = 0;
10447           /* On x86_64 the lea instruction operates on Pmode, so we need
10448              to get arithmetics done in proper mode to match.  */
10449           if (diff == 1)
10450             tmp = copy_rtx (out);
10451           else
10452             {
10453               rtx out1;
10454               out1 = copy_rtx (out);
10455               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
10456               nops++;
10457               if (diff & 1)
10458                 {
10459                   tmp = gen_rtx_PLUS (mode, tmp, out1);
10460                   nops++;
10461                 }
10462             }
10463           if (cf != 0)
10464             {
10465               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
10466               nops++;
10467             }
10468           if (!rtx_equal_p (tmp, out))
10469             {
10470               if (nops == 1)
10471                 out = force_operand (tmp, copy_rtx (out));
10472               else
10473                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
10474             }
10475           if (!rtx_equal_p (out, operands[0]))
10476             emit_move_insn (operands[0], copy_rtx (out));
10477
10478           return 1; /* DONE */
10479         }
10480
10481       /*
10482        * General case:                  Jumpful:
10483        *   xorl dest,dest               cmpl op1, op2
10484        *   cmpl op1, op2                movl ct, dest
10485        *   setcc dest                   jcc 1f
10486        *   decl dest                    movl cf, dest
10487        *   andl (cf-ct),dest            1:
10488        *   addl ct,dest
10489        *
10490        * Size 20.                       Size 14.
10491        *
10492        * This is reasonably steep, but branch mispredict costs are
10493        * high on modern cpus, so consider failing only if optimizing
10494        * for space.
10495        */
10496
10497       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
10498           && BRANCH_COST >= 2)
10499         {
10500           if (cf == 0)
10501             {
10502               cf = ct;
10503               ct = 0;
10504               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
10505                 /* We may be reversing unordered compare to normal compare,
10506                    that is not valid in general (we may convert non-trapping
10507                    condition to trapping one), however on i386 we currently
10508                    emit all comparisons unordered.  */
10509                 code = reverse_condition_maybe_unordered (code);
10510               else
10511                 {
10512                   code = reverse_condition (code);
10513                   if (compare_code != UNKNOWN)
10514                     compare_code = reverse_condition (compare_code);
10515                 }
10516             }
10517
10518           if (compare_code != UNKNOWN)
10519             {
10520               /* notl op1       (if needed)
10521                  sarl $31, op1
10522                  andl (cf-ct), op1
10523                  addl ct, op1
10524
10525                  For x < 0 (resp. x <= -1) there will be no notl,
10526                  so if possible swap the constants to get rid of the
10527                  complement.
10528                  True/false will be -1/0 while code below (store flag
10529                  followed by decrement) is 0/-1, so the constants need
10530                  to be exchanged once more.  */
10531
10532               if (compare_code == GE || !cf)
10533                 {
10534                   code = reverse_condition (code);
10535                   compare_code = LT;
10536                 }
10537               else
10538                 {
10539                   HOST_WIDE_INT tmp = cf;
10540                   cf = ct;
10541                   ct = tmp;
10542                 }
10543
10544               out = emit_store_flag (out, code, ix86_compare_op0,
10545                                      ix86_compare_op1, VOIDmode, 0, -1);
10546             }
10547           else
10548             {
10549               out = emit_store_flag (out, code, ix86_compare_op0,
10550                                      ix86_compare_op1, VOIDmode, 0, 1);
10551
10552               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
10553                                          copy_rtx (out), 1, OPTAB_DIRECT);
10554             }
10555
10556           out = expand_simple_binop (mode, AND, copy_rtx (out),
10557                                      gen_int_mode (cf - ct, mode),
10558                                      copy_rtx (out), 1, OPTAB_DIRECT);
10559           if (ct)
10560             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
10561                                        copy_rtx (out), 1, OPTAB_DIRECT);
10562           if (!rtx_equal_p (out, operands[0]))
10563             emit_move_insn (operands[0], copy_rtx (out));
10564
10565           return 1; /* DONE */
10566         }
10567     }
10568
10569   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
10570     {
10571       /* Try a few things more with specific constants and a variable.  */
10572
10573       optab op;
10574       rtx var, orig_out, out, tmp;
10575
10576       if (BRANCH_COST <= 2)
10577         return 0; /* FAIL */
10578
10579       /* If one of the two operands is an interesting constant, load a
10580          constant with the above and mask it in with a logical operation.  */
10581
10582       if (GET_CODE (operands[2]) == CONST_INT)
10583         {
10584           var = operands[3];
10585           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
10586             operands[3] = constm1_rtx, op = and_optab;
10587           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
10588             operands[3] = const0_rtx, op = ior_optab;
10589           else
10590             return 0; /* FAIL */
10591         }
10592       else if (GET_CODE (operands[3]) == CONST_INT)
10593         {
10594           var = operands[2];
10595           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
10596             operands[2] = constm1_rtx, op = and_optab;
10597           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
10598             operands[2] = const0_rtx, op = ior_optab;
10599           else
10600             return 0; /* FAIL */
10601         }
10602       else
10603         return 0; /* FAIL */
10604
10605       orig_out = operands[0];
10606       tmp = gen_reg_rtx (mode);
10607       operands[0] = tmp;
10608
10609       /* Recurse to get the constant loaded.  */
10610       if (ix86_expand_int_movcc (operands) == 0)
10611         return 0; /* FAIL */
10612
10613       /* Mask in the interesting variable.  */
10614       out = expand_binop (mode, op, var, tmp, orig_out, 0,
10615                           OPTAB_WIDEN);
10616       if (!rtx_equal_p (out, orig_out))
10617         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
10618
10619       return 1; /* DONE */
10620     }
10621
10622   /*
10623    * For comparison with above,
10624    *
10625    * movl cf,dest
10626    * movl ct,tmp
10627    * cmpl op1,op2
10628    * cmovcc tmp,dest
10629    *
10630    * Size 15.
10631    */
10632
10633   if (! nonimmediate_operand (operands[2], mode))
10634     operands[2] = force_reg (mode, operands[2]);
10635   if (! nonimmediate_operand (operands[3], mode))
10636     operands[3] = force_reg (mode, operands[3]);
10637
10638   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
10639     {
10640       rtx tmp = gen_reg_rtx (mode);
10641       emit_move_insn (tmp, operands[3]);
10642       operands[3] = tmp;
10643     }
10644   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
10645     {
10646       rtx tmp = gen_reg_rtx (mode);
10647       emit_move_insn (tmp, operands[2]);
10648       operands[2] = tmp;
10649     }
10650
10651   if (! register_operand (operands[2], VOIDmode)
10652       && (mode == QImode
10653           || ! register_operand (operands[3], VOIDmode)))
10654     operands[2] = force_reg (mode, operands[2]);
10655
10656   if (mode == QImode
10657       && ! register_operand (operands[3], VOIDmode))
10658     operands[3] = force_reg (mode, operands[3]);
10659
10660   emit_insn (compare_seq);
10661   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10662                           gen_rtx_IF_THEN_ELSE (mode,
10663                                                 compare_op, operands[2],
10664                                                 operands[3])));
10665   if (bypass_test)
10666     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
10667                             gen_rtx_IF_THEN_ELSE (mode,
10668                                   bypass_test,
10669                                   copy_rtx (operands[3]),
10670                                   copy_rtx (operands[0]))));
10671   if (second_test)
10672     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
10673                             gen_rtx_IF_THEN_ELSE (mode,
10674                                   second_test,
10675                                   copy_rtx (operands[2]),
10676                                   copy_rtx (operands[0]))));
10677
10678   return 1; /* DONE */
10679 }
10680
10681 /* Swap, force into registers, or otherwise massage the two operands
10682    to an sse comparison with a mask result.  Thus we differ a bit from
10683    ix86_prepare_fp_compare_args which expects to produce a flags result.
10684
10685    The DEST operand exists to help determine whether to commute commutative
10686    operators.  The POP0/POP1 operands are updated in place.  The new
10687    comparison code is returned, or UNKNOWN if not implementable.  */
10688
10689 static enum rtx_code
10690 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
10691                                   rtx *pop0, rtx *pop1)
10692 {
10693   rtx tmp;
10694
10695   switch (code)
10696     {
10697     case LTGT:
10698     case UNEQ:
10699       /* We have no LTGT as an operator.  We could implement it with
10700          NE & ORDERED, but this requires an extra temporary.  It's
10701          not clear that it's worth it.  */
10702       return UNKNOWN;
10703
10704     case LT:
10705     case LE:
10706     case UNGT:
10707     case UNGE:
10708       /* These are supported directly.  */
10709       break;
10710
10711     case EQ:
10712     case NE:
10713     case UNORDERED:
10714     case ORDERED:
10715       /* For commutative operators, try to canonicalize the destination
10716          operand to be first in the comparison - this helps reload to
10717          avoid extra moves.  */
10718       if (!dest || !rtx_equal_p (dest, *pop1))
10719         break;
10720       /* FALLTHRU */
10721
10722     case GE:
10723     case GT:
10724     case UNLE:
10725     case UNLT:
10726       /* These are not supported directly.  Swap the comparison operands
10727          to transform into something that is supported.  */
10728       tmp = *pop0;
10729       *pop0 = *pop1;
10730       *pop1 = tmp;
10731       code = swap_condition (code);
10732       break;
10733
10734     default:
10735       gcc_unreachable ();
10736     }
10737
10738   return code;
10739 }
10740
10741 /* Detect conditional moves that exactly match min/max operational
10742    semantics.  Note that this is IEEE safe, as long as we don't
10743    interchange the operands.
10744
10745    Returns FALSE if this conditional move doesn't match a MIN/MAX,
10746    and TRUE if the operation is successful and instructions are emitted.  */
10747
10748 static bool
10749 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
10750                            rtx cmp_op1, rtx if_true, rtx if_false)
10751 {
10752   enum machine_mode mode;
10753   bool is_min;
10754   rtx tmp;
10755
10756   if (code == LT)
10757     ;
10758   else if (code == UNGE)
10759     {
10760       tmp = if_true;
10761       if_true = if_false;
10762       if_false = tmp;
10763     }
10764   else
10765     return false;
10766
10767   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
10768     is_min = true;
10769   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
10770     is_min = false;
10771   else
10772     return false;
10773
10774   mode = GET_MODE (dest);
10775
10776   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
10777      but MODE may be a vector mode and thus not appropriate.  */
10778   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
10779     {
10780       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
10781       rtvec v;
10782
10783       if_true = force_reg (mode, if_true);
10784       v = gen_rtvec (2, if_true, if_false);
10785       tmp = gen_rtx_UNSPEC (mode, v, u);
10786     }
10787   else
10788     {
10789       code = is_min ? SMIN : SMAX;
10790       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
10791     }
10792
10793   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
10794   return true;
10795 }
10796
10797 /* Expand an sse vector comparison.  Return the register with the result.  */
10798
10799 static rtx
10800 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
10801                      rtx op_true, rtx op_false)
10802 {
10803   enum machine_mode mode = GET_MODE (dest);
10804   rtx x;
10805
10806   cmp_op0 = force_reg (mode, cmp_op0);
10807   if (!nonimmediate_operand (cmp_op1, mode))
10808     cmp_op1 = force_reg (mode, cmp_op1);
10809
10810   if (optimize
10811       || reg_overlap_mentioned_p (dest, op_true)
10812       || reg_overlap_mentioned_p (dest, op_false))
10813     dest = gen_reg_rtx (mode);
10814
10815   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
10816   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10817
10818   return dest;
10819 }
10820
10821 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
10822    operations.  This is used for both scalar and vector conditional moves.  */
10823
10824 static void
10825 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
10826 {
10827   enum machine_mode mode = GET_MODE (dest);
10828   rtx t2, t3, x;
10829
10830   if (op_false == CONST0_RTX (mode))
10831     {
10832       op_true = force_reg (mode, op_true);
10833       x = gen_rtx_AND (mode, cmp, op_true);
10834       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10835     }
10836   else if (op_true == CONST0_RTX (mode))
10837     {
10838       op_false = force_reg (mode, op_false);
10839       x = gen_rtx_NOT (mode, cmp);
10840       x = gen_rtx_AND (mode, x, op_false);
10841       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10842     }
10843   else
10844     {
10845       op_true = force_reg (mode, op_true);
10846       op_false = force_reg (mode, op_false);
10847
10848       t2 = gen_reg_rtx (mode);
10849       if (optimize)
10850         t3 = gen_reg_rtx (mode);
10851       else
10852         t3 = dest;
10853
10854       x = gen_rtx_AND (mode, op_true, cmp);
10855       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
10856
10857       x = gen_rtx_NOT (mode, cmp);
10858       x = gen_rtx_AND (mode, x, op_false);
10859       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
10860
10861       x = gen_rtx_IOR (mode, t3, t2);
10862       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10863     }
10864 }
10865
10866 /* Expand a floating-point conditional move.  Return true if successful.  */
10867
10868 int
10869 ix86_expand_fp_movcc (rtx operands[])
10870 {
10871   enum machine_mode mode = GET_MODE (operands[0]);
10872   enum rtx_code code = GET_CODE (operands[1]);
10873   rtx tmp, compare_op, second_test, bypass_test;
10874
10875   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
10876     {
10877       enum machine_mode cmode;
10878
10879       /* Since we've no cmove for sse registers, don't force bad register
10880          allocation just to gain access to it.  Deny movcc when the
10881          comparison mode doesn't match the move mode.  */
10882       cmode = GET_MODE (ix86_compare_op0);
10883       if (cmode == VOIDmode)
10884         cmode = GET_MODE (ix86_compare_op1);
10885       if (cmode != mode)
10886         return 0;
10887
10888       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
10889                                                &ix86_compare_op0,
10890                                                &ix86_compare_op1);
10891       if (code == UNKNOWN)
10892         return 0;
10893
10894       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
10895                                      ix86_compare_op1, operands[2],
10896                                      operands[3]))
10897         return 1;
10898
10899       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
10900                                  ix86_compare_op1, operands[2], operands[3]);
10901       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
10902       return 1;
10903     }
10904
10905   /* The floating point conditional move instructions don't directly
10906      support conditions resulting from a signed integer comparison.  */
10907
10908   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
10909
10910   /* The floating point conditional move instructions don't directly
10911      support signed integer comparisons.  */
10912
10913   if (!fcmov_comparison_operator (compare_op, VOIDmode))
10914     {
10915       gcc_assert (!second_test && !bypass_test);
10916       tmp = gen_reg_rtx (QImode);
10917       ix86_expand_setcc (code, tmp);
10918       code = NE;
10919       ix86_compare_op0 = tmp;
10920       ix86_compare_op1 = const0_rtx;
10921       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
10922     }
10923   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
10924     {
10925       tmp = gen_reg_rtx (mode);
10926       emit_move_insn (tmp, operands[3]);
10927       operands[3] = tmp;
10928     }
10929   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
10930     {
10931       tmp = gen_reg_rtx (mode);
10932       emit_move_insn (tmp, operands[2]);
10933       operands[2] = tmp;
10934     }
10935
10936   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10937                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
10938                                                 operands[2], operands[3])));
10939   if (bypass_test)
10940     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10941                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
10942                                                   operands[3], operands[0])));
10943   if (second_test)
10944     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10945                             gen_rtx_IF_THEN_ELSE (mode, second_test,
10946                                                   operands[2], operands[0])));
10947
10948   return 1;
10949 }
10950
10951 /* Expand a floating-point vector conditional move; a vcond operation
10952    rather than a movcc operation.  */
10953
10954 bool
10955 ix86_expand_fp_vcond (rtx operands[])
10956 {
10957   enum rtx_code code = GET_CODE (operands[3]);
10958   rtx cmp;
10959
10960   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
10961                                            &operands[4], &operands[5]);
10962   if (code == UNKNOWN)
10963     return false;
10964
10965   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
10966                                  operands[5], operands[1], operands[2]))
10967     return true;
10968
10969   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
10970                              operands[1], operands[2]);
10971   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
10972   return true;
10973 }
10974
10975 /* Expand a signed integral vector conditional move.  */
10976
10977 bool
10978 ix86_expand_int_vcond (rtx operands[])
10979 {
10980   enum machine_mode mode = GET_MODE (operands[0]);
10981   enum rtx_code code = GET_CODE (operands[3]);
10982   bool negate = false;
10983   rtx x, cop0, cop1;
10984
10985   cop0 = operands[4];
10986   cop1 = operands[5];
10987
10988   /* Canonicalize the comparison to EQ, GT, GTU.  */
10989   switch (code)
10990     {
10991     case EQ:
10992     case GT:
10993     case GTU:
10994       break;
10995
10996     case NE:
10997     case LE:
10998     case LEU:
10999       code = reverse_condition (code);
11000       negate = true;
11001       break;
11002
11003     case GE:
11004     case GEU:
11005       code = reverse_condition (code);
11006       negate = true;
11007       /* FALLTHRU */
11008
11009     case LT:
11010     case LTU:
11011       code = swap_condition (code);
11012       x = cop0, cop0 = cop1, cop1 = x;
11013       break;
11014
11015     default:
11016       gcc_unreachable ();
11017     }
11018
11019   /* Unsigned parallel compare is not supported by the hardware.  Play some
11020      tricks to turn this into a signed comparison against 0.  */
11021   if (code == GTU)
11022     {
11023       switch (mode)
11024         {
11025         case V4SImode:
11026           {
11027             rtx t1, t2, mask;
11028
11029             /* Perform a parallel modulo subtraction.  */
11030             t1 = gen_reg_rtx (mode);
11031             emit_insn (gen_subv4si3 (t1, cop0, cop1));
11032
11033             /* Extract the original sign bit of op0.  */
11034             mask = GEN_INT (-0x80000000);
11035             mask = gen_rtx_CONST_VECTOR (mode,
11036                         gen_rtvec (4, mask, mask, mask, mask));
11037             mask = force_reg (mode, mask);
11038             t2 = gen_reg_rtx (mode);
11039             emit_insn (gen_andv4si3 (t2, cop0, mask));
11040
11041             /* XOR it back into the result of the subtraction.  This results
11042                in the sign bit set iff we saw unsigned underflow.  */
11043             x = gen_reg_rtx (mode);
11044             emit_insn (gen_xorv4si3 (x, t1, t2));
11045
11046             code = GT;
11047           }
11048           break;
11049
11050         case V16QImode:
11051         case V8HImode:
11052           /* Perform a parallel unsigned saturating subtraction.  */
11053           x = gen_reg_rtx (mode);
11054           emit_insn (gen_rtx_SET (VOIDmode, x,
11055                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
11056
11057           code = EQ;
11058           negate = !negate;
11059           break;
11060
11061         default:
11062           gcc_unreachable ();
11063         }
11064
11065       cop0 = x;
11066       cop1 = CONST0_RTX (mode);
11067     }
11068
11069   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
11070                            operands[1+negate], operands[2-negate]);
11071
11072   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
11073                          operands[2-negate]);
11074   return true;
11075 }
11076
11077 /* Expand conditional increment or decrement using adb/sbb instructions.
11078    The default case using setcc followed by the conditional move can be
11079    done by generic code.  */
11080 int
11081 ix86_expand_int_addcc (rtx operands[])
11082 {
11083   enum rtx_code code = GET_CODE (operands[1]);
11084   rtx compare_op;
11085   rtx val = const0_rtx;
11086   bool fpcmp = false;
11087   enum machine_mode mode = GET_MODE (operands[0]);
11088
11089   if (operands[3] != const1_rtx
11090       && operands[3] != constm1_rtx)
11091     return 0;
11092   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11093                                        ix86_compare_op1, &compare_op))
11094      return 0;
11095   code = GET_CODE (compare_op);
11096
11097   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11098       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11099     {
11100       fpcmp = true;
11101       code = ix86_fp_compare_code_to_integer (code);
11102     }
11103
11104   if (code != LTU)
11105     {
11106       val = constm1_rtx;
11107       if (fpcmp)
11108         PUT_CODE (compare_op,
11109                   reverse_condition_maybe_unordered
11110                     (GET_CODE (compare_op)));
11111       else
11112         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11113     }
11114   PUT_MODE (compare_op, mode);
11115
11116   /* Construct either adc or sbb insn.  */
11117   if ((code == LTU) == (operands[3] == constm1_rtx))
11118     {
11119       switch (GET_MODE (operands[0]))
11120         {
11121           case QImode:
11122             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
11123             break;
11124           case HImode:
11125             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
11126             break;
11127           case SImode:
11128             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
11129             break;
11130           case DImode:
11131             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11132             break;
11133           default:
11134             gcc_unreachable ();
11135         }
11136     }
11137   else
11138     {
11139       switch (GET_MODE (operands[0]))
11140         {
11141           case QImode:
11142             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
11143             break;
11144           case HImode:
11145             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
11146             break;
11147           case SImode:
11148             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
11149             break;
11150           case DImode:
11151             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11152             break;
11153           default:
11154             gcc_unreachable ();
11155         }
11156     }
11157   return 1; /* DONE */
11158 }
11159
11160
11161 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
11162    works for floating pointer parameters and nonoffsetable memories.
11163    For pushes, it returns just stack offsets; the values will be saved
11164    in the right order.  Maximally three parts are generated.  */
11165
11166 static int
11167 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
11168 {
11169   int size;
11170
11171   if (!TARGET_64BIT)
11172     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
11173   else
11174     size = (GET_MODE_SIZE (mode) + 4) / 8;
11175
11176   gcc_assert (GET_CODE (operand) != REG || !MMX_REGNO_P (REGNO (operand)));
11177   gcc_assert (size >= 2 && size <= 3);
11178
11179   /* Optimize constant pool reference to immediates.  This is used by fp
11180      moves, that force all constants to memory to allow combining.  */
11181   if (GET_CODE (operand) == MEM && MEM_READONLY_P (operand))
11182     {
11183       rtx tmp = maybe_get_pool_constant (operand);
11184       if (tmp)
11185         operand = tmp;
11186     }
11187
11188   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
11189     {
11190       /* The only non-offsetable memories we handle are pushes.  */
11191       int ok = push_operand (operand, VOIDmode);
11192       
11193       gcc_assert (ok);
11194       
11195       operand = copy_rtx (operand);
11196       PUT_MODE (operand, Pmode);
11197       parts[0] = parts[1] = parts[2] = operand;
11198       return size;
11199     }
11200
11201   if (GET_CODE (operand) == CONST_VECTOR)
11202     {
11203       enum machine_mode imode = int_mode_for_mode (mode);
11204       /* Caution: if we looked through a constant pool memory above,
11205          the operand may actually have a different mode now.  That's
11206          ok, since we want to pun this all the way back to an integer.  */
11207       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
11208       gcc_assert (operand != NULL);
11209       mode = imode;
11210     }
11211
11212   if (!TARGET_64BIT)
11213     {
11214       if (mode == DImode)
11215         split_di (&operand, 1, &parts[0], &parts[1]);
11216       else
11217         {
11218           if (REG_P (operand))
11219             {
11220               gcc_assert (reload_completed);
11221               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
11222               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
11223               if (size == 3)
11224                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
11225             }
11226           else if (offsettable_memref_p (operand))
11227             {
11228               operand = adjust_address (operand, SImode, 0);
11229               parts[0] = operand;
11230               parts[1] = adjust_address (operand, SImode, 4);
11231               if (size == 3)
11232                 parts[2] = adjust_address (operand, SImode, 8);
11233             }
11234           else if (GET_CODE (operand) == CONST_DOUBLE)
11235             {
11236               REAL_VALUE_TYPE r;
11237               long l[4];
11238
11239               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11240               switch (mode)
11241                 {
11242                 case XFmode:
11243                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
11244                   parts[2] = gen_int_mode (l[2], SImode);
11245                   break;
11246                 case DFmode:
11247                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
11248                   break;
11249                 default:
11250                   gcc_unreachable ();
11251                 }
11252               parts[1] = gen_int_mode (l[1], SImode);
11253               parts[0] = gen_int_mode (l[0], SImode);
11254             }
11255           else
11256             gcc_unreachable ();
11257         }
11258     }
11259   else
11260     {
11261       if (mode == TImode)
11262         split_ti (&operand, 1, &parts[0], &parts[1]);
11263       if (mode == XFmode || mode == TFmode)
11264         {
11265           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
11266           if (REG_P (operand))
11267             {
11268               gcc_assert (reload_completed);
11269               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
11270               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
11271             }
11272           else if (offsettable_memref_p (operand))
11273             {
11274               operand = adjust_address (operand, DImode, 0);
11275               parts[0] = operand;
11276               parts[1] = adjust_address (operand, upper_mode, 8);
11277             }
11278           else if (GET_CODE (operand) == CONST_DOUBLE)
11279             {
11280               REAL_VALUE_TYPE r;
11281               long l[4];
11282
11283               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11284               real_to_target (l, &r, mode);
11285
11286               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
11287               if (HOST_BITS_PER_WIDE_INT >= 64)
11288                 parts[0]
11289                   = gen_int_mode
11290                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
11291                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
11292                        DImode);
11293               else
11294                 parts[0] = immed_double_const (l[0], l[1], DImode);
11295
11296               if (upper_mode == SImode)
11297                 parts[1] = gen_int_mode (l[2], SImode);
11298               else if (HOST_BITS_PER_WIDE_INT >= 64)
11299                 parts[1]
11300                   = gen_int_mode
11301                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
11302                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
11303                        DImode);
11304               else
11305                 parts[1] = immed_double_const (l[2], l[3], DImode);
11306             }
11307           else
11308             gcc_unreachable ();
11309         }
11310     }
11311
11312   return size;
11313 }
11314
11315 /* Emit insns to perform a move or push of DI, DF, and XF values.
11316    Return false when normal moves are needed; true when all required
11317    insns have been emitted.  Operands 2-4 contain the input values
11318    int the correct order; operands 5-7 contain the output values.  */
11319
11320 void
11321 ix86_split_long_move (rtx operands[])
11322 {
11323   rtx part[2][3];
11324   int nparts;
11325   int push = 0;
11326   int collisions = 0;
11327   enum machine_mode mode = GET_MODE (operands[0]);
11328
11329   /* The DFmode expanders may ask us to move double.
11330      For 64bit target this is single move.  By hiding the fact
11331      here we simplify i386.md splitters.  */
11332   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
11333     {
11334       /* Optimize constant pool reference to immediates.  This is used by
11335          fp moves, that force all constants to memory to allow combining.  */
11336
11337       if (GET_CODE (operands[1]) == MEM
11338           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
11339           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
11340         operands[1] = get_pool_constant (XEXP (operands[1], 0));
11341       if (push_operand (operands[0], VOIDmode))
11342         {
11343           operands[0] = copy_rtx (operands[0]);
11344           PUT_MODE (operands[0], Pmode);
11345         }
11346       else
11347         operands[0] = gen_lowpart (DImode, operands[0]);
11348       operands[1] = gen_lowpart (DImode, operands[1]);
11349       emit_move_insn (operands[0], operands[1]);
11350       return;
11351     }
11352
11353   /* The only non-offsettable memory we handle is push.  */
11354   if (push_operand (operands[0], VOIDmode))
11355     push = 1;
11356   else
11357     gcc_assert (GET_CODE (operands[0]) != MEM
11358                 || offsettable_memref_p (operands[0]));
11359
11360   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
11361   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
11362
11363   /* When emitting push, take care for source operands on the stack.  */
11364   if (push && GET_CODE (operands[1]) == MEM
11365       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
11366     {
11367       if (nparts == 3)
11368         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
11369                                      XEXP (part[1][2], 0));
11370       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
11371                                    XEXP (part[1][1], 0));
11372     }
11373
11374   /* We need to do copy in the right order in case an address register
11375      of the source overlaps the destination.  */
11376   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
11377     {
11378       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
11379         collisions++;
11380       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
11381         collisions++;
11382       if (nparts == 3
11383           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
11384         collisions++;
11385
11386       /* Collision in the middle part can be handled by reordering.  */
11387       if (collisions == 1 && nparts == 3
11388           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
11389         {
11390           rtx tmp;
11391           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
11392           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
11393         }
11394
11395       /* If there are more collisions, we can't handle it by reordering.
11396          Do an lea to the last part and use only one colliding move.  */
11397       else if (collisions > 1)
11398         {
11399           rtx base;
11400
11401           collisions = 1;
11402
11403           base = part[0][nparts - 1];
11404
11405           /* Handle the case when the last part isn't valid for lea.
11406              Happens in 64-bit mode storing the 12-byte XFmode.  */
11407           if (GET_MODE (base) != Pmode)
11408             base = gen_rtx_REG (Pmode, REGNO (base));
11409
11410           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
11411           part[1][0] = replace_equiv_address (part[1][0], base);
11412           part[1][1] = replace_equiv_address (part[1][1],
11413                                       plus_constant (base, UNITS_PER_WORD));
11414           if (nparts == 3)
11415             part[1][2] = replace_equiv_address (part[1][2],
11416                                       plus_constant (base, 8));
11417         }
11418     }
11419
11420   if (push)
11421     {
11422       if (!TARGET_64BIT)
11423         {
11424           if (nparts == 3)
11425             {
11426               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
11427                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
11428               emit_move_insn (part[0][2], part[1][2]);
11429             }
11430         }
11431       else
11432         {
11433           /* In 64bit mode we don't have 32bit push available.  In case this is
11434              register, it is OK - we will just use larger counterpart.  We also
11435              retype memory - these comes from attempt to avoid REX prefix on
11436              moving of second half of TFmode value.  */
11437           if (GET_MODE (part[1][1]) == SImode)
11438             {
11439               switch (GET_CODE (part[1][1]))
11440                 {
11441                 case MEM:
11442                   part[1][1] = adjust_address (part[1][1], DImode, 0);
11443                   break;
11444
11445                 case REG:
11446                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
11447                   break;
11448
11449                 default:
11450                   gcc_unreachable ();
11451                 }
11452               
11453               if (GET_MODE (part[1][0]) == SImode)
11454                 part[1][0] = part[1][1];
11455             }
11456         }
11457       emit_move_insn (part[0][1], part[1][1]);
11458       emit_move_insn (part[0][0], part[1][0]);
11459       return;
11460     }
11461
11462   /* Choose correct order to not overwrite the source before it is copied.  */
11463   if ((REG_P (part[0][0])
11464        && REG_P (part[1][1])
11465        && (REGNO (part[0][0]) == REGNO (part[1][1])
11466            || (nparts == 3
11467                && REGNO (part[0][0]) == REGNO (part[1][2]))))
11468       || (collisions > 0
11469           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
11470     {
11471       if (nparts == 3)
11472         {
11473           operands[2] = part[0][2];
11474           operands[3] = part[0][1];
11475           operands[4] = part[0][0];
11476           operands[5] = part[1][2];
11477           operands[6] = part[1][1];
11478           operands[7] = part[1][0];
11479         }
11480       else
11481         {
11482           operands[2] = part[0][1];
11483           operands[3] = part[0][0];
11484           operands[5] = part[1][1];
11485           operands[6] = part[1][0];
11486         }
11487     }
11488   else
11489     {
11490       if (nparts == 3)
11491         {
11492           operands[2] = part[0][0];
11493           operands[3] = part[0][1];
11494           operands[4] = part[0][2];
11495           operands[5] = part[1][0];
11496           operands[6] = part[1][1];
11497           operands[7] = part[1][2];
11498         }
11499       else
11500         {
11501           operands[2] = part[0][0];
11502           operands[3] = part[0][1];
11503           operands[5] = part[1][0];
11504           operands[6] = part[1][1];
11505         }
11506     }
11507
11508   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
11509   if (optimize_size)
11510     {
11511       if (GET_CODE (operands[5]) == CONST_INT
11512           && operands[5] != const0_rtx
11513           && REG_P (operands[2]))
11514         {
11515           if (GET_CODE (operands[6]) == CONST_INT
11516               && INTVAL (operands[6]) == INTVAL (operands[5]))
11517             operands[6] = operands[2];
11518
11519           if (nparts == 3
11520               && GET_CODE (operands[7]) == CONST_INT
11521               && INTVAL (operands[7]) == INTVAL (operands[5]))
11522             operands[7] = operands[2];
11523         }
11524
11525       if (nparts == 3
11526           && GET_CODE (operands[6]) == CONST_INT
11527           && operands[6] != const0_rtx
11528           && REG_P (operands[3])
11529           && GET_CODE (operands[7]) == CONST_INT
11530           && INTVAL (operands[7]) == INTVAL (operands[6]))
11531         operands[7] = operands[3];
11532     }
11533
11534   emit_move_insn (operands[2], operands[5]);
11535   emit_move_insn (operands[3], operands[6]);
11536   if (nparts == 3)
11537     emit_move_insn (operands[4], operands[7]);
11538
11539   return;
11540 }
11541
11542 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
11543    left shift by a constant, either using a single shift or
11544    a sequence of add instructions.  */
11545
11546 static void
11547 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
11548 {
11549   if (count == 1)
11550     {
11551       emit_insn ((mode == DImode
11552                   ? gen_addsi3
11553                   : gen_adddi3) (operand, operand, operand));
11554     }
11555   else if (!optimize_size
11556            && count * ix86_cost->add <= ix86_cost->shift_const)
11557     {
11558       int i;
11559       for (i=0; i<count; i++)
11560         {
11561           emit_insn ((mode == DImode
11562                       ? gen_addsi3
11563                       : gen_adddi3) (operand, operand, operand));
11564         }
11565     }
11566   else
11567     emit_insn ((mode == DImode
11568                 ? gen_ashlsi3
11569                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
11570 }
11571
11572 void
11573 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
11574 {
11575   rtx low[2], high[2];
11576   int count;
11577   const int single_width = mode == DImode ? 32 : 64;
11578
11579   if (GET_CODE (operands[2]) == CONST_INT)
11580     {
11581       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
11582       count = INTVAL (operands[2]) & (single_width * 2 - 1);
11583
11584       if (count >= single_width)
11585         {
11586           emit_move_insn (high[0], low[1]);
11587           emit_move_insn (low[0], const0_rtx);
11588
11589           if (count > single_width)
11590             ix86_expand_ashl_const (high[0], count - single_width, mode);
11591         }
11592       else
11593         {
11594           if (!rtx_equal_p (operands[0], operands[1]))
11595             emit_move_insn (operands[0], operands[1]);
11596           emit_insn ((mode == DImode
11597                      ? gen_x86_shld_1
11598                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
11599           ix86_expand_ashl_const (low[0], count, mode);
11600         }
11601       return;
11602     }
11603
11604   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
11605
11606   if (operands[1] == const1_rtx)
11607     {
11608       /* Assuming we've chosen a QImode capable registers, then 1 << N
11609          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
11610       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
11611         {
11612           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
11613
11614           ix86_expand_clear (low[0]);
11615           ix86_expand_clear (high[0]);
11616           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
11617           
11618           d = gen_lowpart (QImode, low[0]);
11619           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
11620           s = gen_rtx_EQ (QImode, flags, const0_rtx);
11621           emit_insn (gen_rtx_SET (VOIDmode, d, s));
11622
11623           d = gen_lowpart (QImode, high[0]);
11624           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
11625           s = gen_rtx_NE (QImode, flags, const0_rtx);
11626           emit_insn (gen_rtx_SET (VOIDmode, d, s));
11627         }
11628
11629       /* Otherwise, we can get the same results by manually performing
11630          a bit extract operation on bit 5/6, and then performing the two
11631          shifts.  The two methods of getting 0/1 into low/high are exactly
11632          the same size.  Avoiding the shift in the bit extract case helps
11633          pentium4 a bit; no one else seems to care much either way.  */
11634       else
11635         {
11636           rtx x;
11637
11638           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
11639             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
11640           else
11641             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
11642           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
11643
11644           emit_insn ((mode == DImode
11645                       ? gen_lshrsi3
11646                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
11647           emit_insn ((mode == DImode
11648                       ? gen_andsi3
11649                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
11650           emit_move_insn (low[0], high[0]);
11651           emit_insn ((mode == DImode
11652                       ? gen_xorsi3
11653                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
11654         }
11655
11656       emit_insn ((mode == DImode
11657                     ? gen_ashlsi3
11658                     : gen_ashldi3) (low[0], low[0], operands[2]));
11659       emit_insn ((mode == DImode
11660                     ? gen_ashlsi3
11661                     : gen_ashldi3) (high[0], high[0], operands[2]));
11662       return;
11663     }
11664
11665   if (operands[1] == constm1_rtx)
11666     {
11667       /* For -1 << N, we can avoid the shld instruction, because we
11668          know that we're shifting 0...31/63 ones into a -1.  */
11669       emit_move_insn (low[0], constm1_rtx);
11670       if (optimize_size)
11671         emit_move_insn (high[0], low[0]);
11672       else
11673         emit_move_insn (high[0], constm1_rtx);
11674     }
11675   else
11676     {
11677       if (!rtx_equal_p (operands[0], operands[1]))
11678         emit_move_insn (operands[0], operands[1]);
11679
11680       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
11681       emit_insn ((mode == DImode
11682                   ? gen_x86_shld_1
11683                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
11684     }
11685
11686   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
11687
11688   if (TARGET_CMOVE && scratch)
11689     {
11690       ix86_expand_clear (scratch);
11691       emit_insn ((mode == DImode
11692                   ? gen_x86_shift_adj_1
11693                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
11694     }
11695   else
11696     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
11697 }
11698
11699 void
11700 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
11701 {
11702   rtx low[2], high[2];
11703   int count;
11704   const int single_width = mode == DImode ? 32 : 64;
11705
11706   if (GET_CODE (operands[2]) == CONST_INT)
11707     {
11708       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
11709       count = INTVAL (operands[2]) & (single_width * 2 - 1);
11710
11711       if (count == single_width * 2 - 1)
11712         {
11713           emit_move_insn (high[0], high[1]);
11714           emit_insn ((mode == DImode
11715                       ? gen_ashrsi3
11716                       : gen_ashrdi3) (high[0], high[0],
11717                                       GEN_INT (single_width - 1)));
11718           emit_move_insn (low[0], high[0]);
11719
11720         }
11721       else if (count >= single_width)
11722         {
11723           emit_move_insn (low[0], high[1]);
11724           emit_move_insn (high[0], low[0]);
11725           emit_insn ((mode == DImode
11726                       ? gen_ashrsi3
11727                       : gen_ashrdi3) (high[0], high[0],
11728                                       GEN_INT (single_width - 1)));
11729           if (count > single_width)
11730             emit_insn ((mode == DImode
11731                         ? gen_ashrsi3
11732                         : gen_ashrdi3) (low[0], low[0],
11733                                         GEN_INT (count - single_width)));
11734         }
11735       else
11736         {
11737           if (!rtx_equal_p (operands[0], operands[1]))
11738             emit_move_insn (operands[0], operands[1]);
11739           emit_insn ((mode == DImode
11740                       ? gen_x86_shrd_1
11741                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
11742           emit_insn ((mode == DImode
11743                       ? gen_ashrsi3
11744                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
11745         }
11746     }
11747   else
11748     {
11749       if (!rtx_equal_p (operands[0], operands[1]))
11750         emit_move_insn (operands[0], operands[1]);
11751
11752       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
11753
11754       emit_insn ((mode == DImode
11755                   ? gen_x86_shrd_1
11756                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
11757       emit_insn ((mode == DImode
11758                   ? gen_ashrsi3
11759                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
11760
11761       if (TARGET_CMOVE && scratch)
11762         {
11763           emit_move_insn (scratch, high[0]);
11764           emit_insn ((mode == DImode
11765                       ? gen_ashrsi3
11766                       : gen_ashrdi3) (scratch, scratch,
11767                                       GEN_INT (single_width - 1)));
11768           emit_insn ((mode == DImode
11769                       ? gen_x86_shift_adj_1
11770                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
11771                                          scratch));
11772         }
11773       else
11774         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
11775     }
11776 }
11777
11778 void
11779 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
11780 {
11781   rtx low[2], high[2];
11782   int count;
11783   const int single_width = mode == DImode ? 32 : 64;
11784
11785   if (GET_CODE (operands[2]) == CONST_INT)
11786     {
11787       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
11788       count = INTVAL (operands[2]) & (single_width * 2 - 1);
11789
11790       if (count >= single_width)
11791         {
11792           emit_move_insn (low[0], high[1]);
11793           ix86_expand_clear (high[0]);
11794
11795           if (count > single_width)
11796             emit_insn ((mode == DImode
11797                         ? gen_lshrsi3
11798                         : gen_lshrdi3) (low[0], low[0],
11799                                         GEN_INT (count - single_width)));
11800         }
11801       else
11802         {
11803           if (!rtx_equal_p (operands[0], operands[1]))
11804             emit_move_insn (operands[0], operands[1]);
11805           emit_insn ((mode == DImode
11806                       ? gen_x86_shrd_1
11807                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
11808           emit_insn ((mode == DImode
11809                       ? gen_lshrsi3
11810                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
11811         }
11812     }
11813   else
11814     {
11815       if (!rtx_equal_p (operands[0], operands[1]))
11816         emit_move_insn (operands[0], operands[1]);
11817
11818       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
11819
11820       emit_insn ((mode == DImode
11821                   ? gen_x86_shrd_1
11822                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
11823       emit_insn ((mode == DImode
11824                   ? gen_lshrsi3
11825                   : gen_lshrdi3) (high[0], high[0], operands[2]));
11826
11827       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
11828       if (TARGET_CMOVE && scratch)
11829         {
11830           ix86_expand_clear (scratch);
11831           emit_insn ((mode == DImode
11832                       ? gen_x86_shift_adj_1
11833                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
11834                                                scratch));
11835         }
11836       else
11837         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
11838     }
11839 }
11840
11841 /* Helper function for the string operations below.  Dest VARIABLE whether
11842    it is aligned to VALUE bytes.  If true, jump to the label.  */
11843 static rtx
11844 ix86_expand_aligntest (rtx variable, int value)
11845 {
11846   rtx label = gen_label_rtx ();
11847   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
11848   if (GET_MODE (variable) == DImode)
11849     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
11850   else
11851     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
11852   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
11853                            1, label);
11854   return label;
11855 }
11856
11857 /* Adjust COUNTER by the VALUE.  */
11858 static void
11859 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
11860 {
11861   if (GET_MODE (countreg) == DImode)
11862     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
11863   else
11864     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
11865 }
11866
11867 /* Zero extend possibly SImode EXP to Pmode register.  */
11868 rtx
11869 ix86_zero_extend_to_Pmode (rtx exp)
11870 {
11871   rtx r;
11872   if (GET_MODE (exp) == VOIDmode)
11873     return force_reg (Pmode, exp);
11874   if (GET_MODE (exp) == Pmode)
11875     return copy_to_mode_reg (Pmode, exp);
11876   r = gen_reg_rtx (Pmode);
11877   emit_insn (gen_zero_extendsidi2 (r, exp));
11878   return r;
11879 }
11880
11881 /* Expand string move (memcpy) operation.  Use i386 string operations when
11882    profitable.  expand_clrmem contains similar code.  */
11883 int
11884 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
11885 {
11886   rtx srcreg, destreg, countreg, srcexp, destexp;
11887   enum machine_mode counter_mode;
11888   HOST_WIDE_INT align = 0;
11889   unsigned HOST_WIDE_INT count = 0;
11890
11891   if (GET_CODE (align_exp) == CONST_INT)
11892     align = INTVAL (align_exp);
11893
11894   /* Can't use any of this if the user has appropriated esi or edi.  */
11895   if (global_regs[4] || global_regs[5])
11896     return 0;
11897
11898   /* This simple hack avoids all inlining code and simplifies code below.  */
11899   if (!TARGET_ALIGN_STRINGOPS)
11900     align = 64;
11901
11902   if (GET_CODE (count_exp) == CONST_INT)
11903     {
11904       count = INTVAL (count_exp);
11905       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
11906         return 0;
11907     }
11908
11909   /* Figure out proper mode for counter.  For 32bits it is always SImode,
11910      for 64bits use SImode when possible, otherwise DImode.
11911      Set count to number of bytes copied when known at compile time.  */
11912   if (!TARGET_64BIT
11913       || GET_MODE (count_exp) == SImode
11914       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
11915     counter_mode = SImode;
11916   else
11917     counter_mode = DImode;
11918
11919   gcc_assert (counter_mode == SImode || counter_mode == DImode);
11920
11921   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
11922   if (destreg != XEXP (dst, 0))
11923     dst = replace_equiv_address_nv (dst, destreg);
11924   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
11925   if (srcreg != XEXP (src, 0))
11926     src = replace_equiv_address_nv (src, srcreg);
11927
11928   /* When optimizing for size emit simple rep ; movsb instruction for
11929      counts not divisible by 4, except when (movsl;)*(movsw;)?(movsb;)?
11930      sequence is shorter than mov{b,l} $count, %{ecx,cl}; rep; movsb.
11931      Sice of (movsl;)*(movsw;)?(movsb;)? sequence is
11932      count / 4 + (count & 3), the other sequence is either 4 or 7 bytes,
11933      but we don't know whether upper 24 (resp. 56) bits of %ecx will be
11934      known to be zero or not.  The rep; movsb sequence causes higher
11935      register pressure though, so take that into account.  */
11936
11937   if ((!optimize || optimize_size)
11938       && (count == 0
11939           || ((count & 0x03)
11940               && (!optimize_size
11941                   || count > 5 * 4
11942                   || (count & 3) + count / 4 > 6))))
11943     {
11944       emit_insn (gen_cld ());
11945       countreg = ix86_zero_extend_to_Pmode (count_exp);
11946       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
11947       srcexp = gen_rtx_PLUS (Pmode, srcreg, countreg);
11948       emit_insn (gen_rep_mov (destreg, dst, srcreg, src, countreg,
11949                               destexp, srcexp));
11950     }
11951
11952   /* For constant aligned (or small unaligned) copies use rep movsl
11953      followed by code copying the rest.  For PentiumPro ensure 8 byte
11954      alignment to allow rep movsl acceleration.  */
11955
11956   else if (count != 0
11957            && (align >= 8
11958                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
11959                || optimize_size || count < (unsigned int) 64))
11960     {
11961       unsigned HOST_WIDE_INT offset = 0;
11962       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
11963       rtx srcmem, dstmem;
11964
11965       emit_insn (gen_cld ());
11966       if (count & ~(size - 1))
11967         {
11968           if ((TARGET_SINGLE_STRINGOP || optimize_size) && count < 5 * 4)
11969             {
11970               enum machine_mode movs_mode = size == 4 ? SImode : DImode;
11971
11972               while (offset < (count & ~(size - 1)))
11973                 {
11974                   srcmem = adjust_automodify_address_nv (src, movs_mode,
11975                                                          srcreg, offset);
11976                   dstmem = adjust_automodify_address_nv (dst, movs_mode,
11977                                                          destreg, offset);
11978                   emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11979                   offset += size;
11980                 }
11981             }
11982           else
11983             {
11984               countreg = GEN_INT ((count >> (size == 4 ? 2 : 3))
11985                                   & (TARGET_64BIT ? -1 : 0x3fffffff));
11986               countreg = copy_to_mode_reg (counter_mode, countreg);
11987               countreg = ix86_zero_extend_to_Pmode (countreg);
11988
11989               destexp = gen_rtx_ASHIFT (Pmode, countreg,
11990                                         GEN_INT (size == 4 ? 2 : 3));
11991               srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
11992               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
11993
11994               emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
11995                                       countreg, destexp, srcexp));
11996               offset = count & ~(size - 1);
11997             }
11998         }
11999       if (size == 8 && (count & 0x04))
12000         {
12001           srcmem = adjust_automodify_address_nv (src, SImode, srcreg,
12002                                                  offset);
12003           dstmem = adjust_automodify_address_nv (dst, SImode, destreg,
12004                                                  offset);
12005           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12006           offset += 4;
12007         }
12008       if (count & 0x02)
12009         {
12010           srcmem = adjust_automodify_address_nv (src, HImode, srcreg,
12011                                                  offset);
12012           dstmem = adjust_automodify_address_nv (dst, HImode, destreg,
12013                                                  offset);
12014           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12015           offset += 2;
12016         }
12017       if (count & 0x01)
12018         {
12019           srcmem = adjust_automodify_address_nv (src, QImode, srcreg,
12020                                                  offset);
12021           dstmem = adjust_automodify_address_nv (dst, QImode, destreg,
12022                                                  offset);
12023           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12024         }
12025     }
12026   /* The generic code based on the glibc implementation:
12027      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
12028      allowing accelerated copying there)
12029      - copy the data using rep movsl
12030      - copy the rest.  */
12031   else
12032     {
12033       rtx countreg2;
12034       rtx label = NULL;
12035       rtx srcmem, dstmem;
12036       int desired_alignment = (TARGET_PENTIUMPRO
12037                                && (count == 0 || count >= (unsigned int) 260)
12038                                ? 8 : UNITS_PER_WORD);
12039       /* Get rid of MEM_OFFSETs, they won't be accurate.  */
12040       dst = change_address (dst, BLKmode, destreg);
12041       src = change_address (src, BLKmode, srcreg);
12042
12043       /* In case we don't know anything about the alignment, default to
12044          library version, since it is usually equally fast and result in
12045          shorter code.
12046
12047          Also emit call when we know that the count is large and call overhead
12048          will not be important.  */
12049       if (!TARGET_INLINE_ALL_STRINGOPS
12050           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12051         return 0;
12052
12053       if (TARGET_SINGLE_STRINGOP)
12054         emit_insn (gen_cld ());
12055
12056       countreg2 = gen_reg_rtx (Pmode);
12057       countreg = copy_to_mode_reg (counter_mode, count_exp);
12058
12059       /* We don't use loops to align destination and to copy parts smaller
12060          than 4 bytes, because gcc is able to optimize such code better (in
12061          the case the destination or the count really is aligned, gcc is often
12062          able to predict the branches) and also it is friendlier to the
12063          hardware branch prediction.
12064
12065          Using loops is beneficial for generic case, because we can
12066          handle small counts using the loops.  Many CPUs (such as Athlon)
12067          have large REP prefix setup costs.
12068
12069          This is quite costly.  Maybe we can revisit this decision later or
12070          add some customizability to this code.  */
12071
12072       if (count == 0 && align < desired_alignment)
12073         {
12074           label = gen_label_rtx ();
12075           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
12076                                    LEU, 0, counter_mode, 1, label);
12077         }
12078       if (align <= 1)
12079         {
12080           rtx label = ix86_expand_aligntest (destreg, 1);
12081           srcmem = change_address (src, QImode, srcreg);
12082           dstmem = change_address (dst, QImode, destreg);
12083           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12084           ix86_adjust_counter (countreg, 1);
12085           emit_label (label);
12086           LABEL_NUSES (label) = 1;
12087         }
12088       if (align <= 2)
12089         {
12090           rtx label = ix86_expand_aligntest (destreg, 2);
12091           srcmem = change_address (src, HImode, srcreg);
12092           dstmem = change_address (dst, HImode, destreg);
12093           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12094           ix86_adjust_counter (countreg, 2);
12095           emit_label (label);
12096           LABEL_NUSES (label) = 1;
12097         }
12098       if (align <= 4 && desired_alignment > 4)
12099         {
12100           rtx label = ix86_expand_aligntest (destreg, 4);
12101           srcmem = change_address (src, SImode, srcreg);
12102           dstmem = change_address (dst, SImode, destreg);
12103           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12104           ix86_adjust_counter (countreg, 4);
12105           emit_label (label);
12106           LABEL_NUSES (label) = 1;
12107         }
12108
12109       if (label && desired_alignment > 4 && !TARGET_64BIT)
12110         {
12111           emit_label (label);
12112           LABEL_NUSES (label) = 1;
12113           label = NULL_RTX;
12114         }
12115       if (!TARGET_SINGLE_STRINGOP)
12116         emit_insn (gen_cld ());
12117       if (TARGET_64BIT)
12118         {
12119           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
12120                                   GEN_INT (3)));
12121           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
12122         }
12123       else
12124         {
12125           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
12126           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
12127         }
12128       srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
12129       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12130       emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
12131                               countreg2, destexp, srcexp));
12132
12133       if (label)
12134         {
12135           emit_label (label);
12136           LABEL_NUSES (label) = 1;
12137         }
12138       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
12139         {
12140           srcmem = change_address (src, SImode, srcreg);
12141           dstmem = change_address (dst, SImode, destreg);
12142           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12143         }
12144       if ((align <= 4 || count == 0) && TARGET_64BIT)
12145         {
12146           rtx label = ix86_expand_aligntest (countreg, 4);
12147           srcmem = change_address (src, SImode, srcreg);
12148           dstmem = change_address (dst, SImode, destreg);
12149           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12150           emit_label (label);
12151           LABEL_NUSES (label) = 1;
12152         }
12153       if (align > 2 && count != 0 && (count & 2))
12154         {
12155           srcmem = change_address (src, HImode, srcreg);
12156           dstmem = change_address (dst, HImode, destreg);
12157           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12158         }
12159       if (align <= 2 || count == 0)
12160         {
12161           rtx label = ix86_expand_aligntest (countreg, 2);
12162           srcmem = change_address (src, HImode, srcreg);
12163           dstmem = change_address (dst, HImode, destreg);
12164           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12165           emit_label (label);
12166           LABEL_NUSES (label) = 1;
12167         }
12168       if (align > 1 && count != 0 && (count & 1))
12169         {
12170           srcmem = change_address (src, QImode, srcreg);
12171           dstmem = change_address (dst, QImode, destreg);
12172           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12173         }
12174       if (align <= 1 || count == 0)
12175         {
12176           rtx label = ix86_expand_aligntest (countreg, 1);
12177           srcmem = change_address (src, QImode, srcreg);
12178           dstmem = change_address (dst, QImode, destreg);
12179           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12180           emit_label (label);
12181           LABEL_NUSES (label) = 1;
12182         }
12183     }
12184
12185   return 1;
12186 }
12187
12188 /* Expand string clear operation (bzero).  Use i386 string operations when
12189    profitable.  expand_movmem contains similar code.  */
12190 int
12191 ix86_expand_clrmem (rtx dst, rtx count_exp, rtx align_exp)
12192 {
12193   rtx destreg, zeroreg, countreg, destexp;
12194   enum machine_mode counter_mode;
12195   HOST_WIDE_INT align = 0;
12196   unsigned HOST_WIDE_INT count = 0;
12197
12198   if (GET_CODE (align_exp) == CONST_INT)
12199     align = INTVAL (align_exp);
12200
12201   /* Can't use any of this if the user has appropriated esi.  */
12202   if (global_regs[4])
12203     return 0;
12204
12205   /* This simple hack avoids all inlining code and simplifies code below.  */
12206   if (!TARGET_ALIGN_STRINGOPS)
12207     align = 32;
12208
12209   if (GET_CODE (count_exp) == CONST_INT)
12210     {
12211       count = INTVAL (count_exp);
12212       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
12213         return 0;
12214     }
12215   /* Figure out proper mode for counter.  For 32bits it is always SImode,
12216      for 64bits use SImode when possible, otherwise DImode.
12217      Set count to number of bytes copied when known at compile time.  */
12218   if (!TARGET_64BIT
12219       || GET_MODE (count_exp) == SImode
12220       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
12221     counter_mode = SImode;
12222   else
12223     counter_mode = DImode;
12224
12225   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
12226   if (destreg != XEXP (dst, 0))
12227     dst = replace_equiv_address_nv (dst, destreg);
12228
12229
12230   /* When optimizing for size emit simple rep ; movsb instruction for
12231      counts not divisible by 4.  The movl $N, %ecx; rep; stosb
12232      sequence is 7 bytes long, so if optimizing for size and count is
12233      small enough that some stosl, stosw and stosb instructions without
12234      rep are shorter, fall back into the next if.  */
12235
12236   if ((!optimize || optimize_size)
12237       && (count == 0
12238           || ((count & 0x03)
12239               && (!optimize_size || (count & 0x03) + (count >> 2) > 7))))
12240     {
12241       emit_insn (gen_cld ());
12242
12243       countreg = ix86_zero_extend_to_Pmode (count_exp);
12244       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
12245       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
12246       emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg, destexp));
12247     }
12248   else if (count != 0
12249            && (align >= 8
12250                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
12251                || optimize_size || count < (unsigned int) 64))
12252     {
12253       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
12254       unsigned HOST_WIDE_INT offset = 0;
12255
12256       emit_insn (gen_cld ());
12257
12258       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
12259       if (count & ~(size - 1))
12260         {
12261           unsigned HOST_WIDE_INT repcount;
12262           unsigned int max_nonrep;
12263
12264           repcount = count >> (size == 4 ? 2 : 3);
12265           if (!TARGET_64BIT)
12266             repcount &= 0x3fffffff;
12267
12268           /* movl $N, %ecx; rep; stosl is 7 bytes, while N x stosl is N bytes.
12269              movl $N, %ecx; rep; stosq is 8 bytes, while N x stosq is 2xN
12270              bytes.  In both cases the latter seems to be faster for small
12271              values of N.  */
12272           max_nonrep = size == 4 ? 7 : 4;
12273           if (!optimize_size)
12274             switch (ix86_tune)
12275               {
12276               case PROCESSOR_PENTIUM4:
12277               case PROCESSOR_NOCONA:
12278                 max_nonrep = 3;
12279                 break;
12280               default:
12281                 break;
12282               }
12283
12284           if (repcount <= max_nonrep)
12285             while (repcount-- > 0)
12286               {
12287                 rtx mem = adjust_automodify_address_nv (dst,
12288                                                         GET_MODE (zeroreg),
12289                                                         destreg, offset);
12290                 emit_insn (gen_strset (destreg, mem, zeroreg));
12291                 offset += size;
12292               }
12293           else
12294             {
12295               countreg = copy_to_mode_reg (counter_mode, GEN_INT (repcount));
12296               countreg = ix86_zero_extend_to_Pmode (countreg);
12297               destexp = gen_rtx_ASHIFT (Pmode, countreg,
12298                                         GEN_INT (size == 4 ? 2 : 3));
12299               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12300               emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg,
12301                                        destexp));
12302               offset = count & ~(size - 1);
12303             }
12304         }
12305       if (size == 8 && (count & 0x04))
12306         {
12307           rtx mem = adjust_automodify_address_nv (dst, SImode, destreg,
12308                                                   offset);
12309           emit_insn (gen_strset (destreg, mem,
12310                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
12311           offset += 4;
12312         }
12313       if (count & 0x02)
12314         {
12315           rtx mem = adjust_automodify_address_nv (dst, HImode, destreg,
12316                                                   offset);
12317           emit_insn (gen_strset (destreg, mem,
12318                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12319           offset += 2;
12320         }
12321       if (count & 0x01)
12322         {
12323           rtx mem = adjust_automodify_address_nv (dst, QImode, destreg,
12324                                                   offset);
12325           emit_insn (gen_strset (destreg, mem,
12326                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12327         }
12328     }
12329   else
12330     {
12331       rtx countreg2;
12332       rtx label = NULL;
12333       /* Compute desired alignment of the string operation.  */
12334       int desired_alignment = (TARGET_PENTIUMPRO
12335                                && (count == 0 || count >= (unsigned int) 260)
12336                                ? 8 : UNITS_PER_WORD);
12337
12338       /* In case we don't know anything about the alignment, default to
12339          library version, since it is usually equally fast and result in
12340          shorter code.
12341
12342          Also emit call when we know that the count is large and call overhead
12343          will not be important.  */
12344       if (!TARGET_INLINE_ALL_STRINGOPS
12345           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12346         return 0;
12347
12348       if (TARGET_SINGLE_STRINGOP)
12349         emit_insn (gen_cld ());
12350
12351       countreg2 = gen_reg_rtx (Pmode);
12352       countreg = copy_to_mode_reg (counter_mode, count_exp);
12353       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
12354       /* Get rid of MEM_OFFSET, it won't be accurate.  */
12355       dst = change_address (dst, BLKmode, destreg);
12356
12357       if (count == 0 && align < desired_alignment)
12358         {
12359           label = gen_label_rtx ();
12360           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
12361                                    LEU, 0, counter_mode, 1, label);
12362         }
12363       if (align <= 1)
12364         {
12365           rtx label = ix86_expand_aligntest (destreg, 1);
12366           emit_insn (gen_strset (destreg, dst,
12367                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12368           ix86_adjust_counter (countreg, 1);
12369           emit_label (label);
12370           LABEL_NUSES (label) = 1;
12371         }
12372       if (align <= 2)
12373         {
12374           rtx label = ix86_expand_aligntest (destreg, 2);
12375           emit_insn (gen_strset (destreg, dst,
12376                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12377           ix86_adjust_counter (countreg, 2);
12378           emit_label (label);
12379           LABEL_NUSES (label) = 1;
12380         }
12381       if (align <= 4 && desired_alignment > 4)
12382         {
12383           rtx label = ix86_expand_aligntest (destreg, 4);
12384           emit_insn (gen_strset (destreg, dst,
12385                                  (TARGET_64BIT
12386                                   ? gen_rtx_SUBREG (SImode, zeroreg, 0)
12387                                   : zeroreg)));
12388           ix86_adjust_counter (countreg, 4);
12389           emit_label (label);
12390           LABEL_NUSES (label) = 1;
12391         }
12392
12393       if (label && desired_alignment > 4 && !TARGET_64BIT)
12394         {
12395           emit_label (label);
12396           LABEL_NUSES (label) = 1;
12397           label = NULL_RTX;
12398         }
12399
12400       if (!TARGET_SINGLE_STRINGOP)
12401         emit_insn (gen_cld ());
12402       if (TARGET_64BIT)
12403         {
12404           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
12405                                   GEN_INT (3)));
12406           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
12407         }
12408       else
12409         {
12410           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
12411           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
12412         }
12413       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12414       emit_insn (gen_rep_stos (destreg, countreg2, dst, zeroreg, destexp));
12415
12416       if (label)
12417         {
12418           emit_label (label);
12419           LABEL_NUSES (label) = 1;
12420         }
12421
12422       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
12423         emit_insn (gen_strset (destreg, dst,
12424                                gen_rtx_SUBREG (SImode, zeroreg, 0)));
12425       if (TARGET_64BIT && (align <= 4 || count == 0))
12426         {
12427           rtx label = ix86_expand_aligntest (countreg, 4);
12428           emit_insn (gen_strset (destreg, dst,
12429                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
12430           emit_label (label);
12431           LABEL_NUSES (label) = 1;
12432         }
12433       if (align > 2 && count != 0 && (count & 2))
12434         emit_insn (gen_strset (destreg, dst,
12435                                gen_rtx_SUBREG (HImode, zeroreg, 0)));
12436       if (align <= 2 || count == 0)
12437         {
12438           rtx label = ix86_expand_aligntest (countreg, 2);
12439           emit_insn (gen_strset (destreg, dst,
12440                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12441           emit_label (label);
12442           LABEL_NUSES (label) = 1;
12443         }
12444       if (align > 1 && count != 0 && (count & 1))
12445         emit_insn (gen_strset (destreg, dst,
12446                                gen_rtx_SUBREG (QImode, zeroreg, 0)));
12447       if (align <= 1 || count == 0)
12448         {
12449           rtx label = ix86_expand_aligntest (countreg, 1);
12450           emit_insn (gen_strset (destreg, dst,
12451                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12452           emit_label (label);
12453           LABEL_NUSES (label) = 1;
12454         }
12455     }
12456   return 1;
12457 }
12458
12459 /* Expand strlen.  */
12460 int
12461 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
12462 {
12463   rtx addr, scratch1, scratch2, scratch3, scratch4;
12464
12465   /* The generic case of strlen expander is long.  Avoid it's
12466      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
12467
12468   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
12469       && !TARGET_INLINE_ALL_STRINGOPS
12470       && !optimize_size
12471       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
12472     return 0;
12473
12474   addr = force_reg (Pmode, XEXP (src, 0));
12475   scratch1 = gen_reg_rtx (Pmode);
12476
12477   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
12478       && !optimize_size)
12479     {
12480       /* Well it seems that some optimizer does not combine a call like
12481          foo(strlen(bar), strlen(bar));
12482          when the move and the subtraction is done here.  It does calculate
12483          the length just once when these instructions are done inside of
12484          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
12485          often used and I use one fewer register for the lifetime of
12486          output_strlen_unroll() this is better.  */
12487
12488       emit_move_insn (out, addr);
12489
12490       ix86_expand_strlensi_unroll_1 (out, src, align);
12491
12492       /* strlensi_unroll_1 returns the address of the zero at the end of
12493          the string, like memchr(), so compute the length by subtracting
12494          the start address.  */
12495       if (TARGET_64BIT)
12496         emit_insn (gen_subdi3 (out, out, addr));
12497       else
12498         emit_insn (gen_subsi3 (out, out, addr));
12499     }
12500   else
12501     {
12502       rtx unspec;
12503       scratch2 = gen_reg_rtx (Pmode);
12504       scratch3 = gen_reg_rtx (Pmode);
12505       scratch4 = force_reg (Pmode, constm1_rtx);
12506
12507       emit_move_insn (scratch3, addr);
12508       eoschar = force_reg (QImode, eoschar);
12509
12510       emit_insn (gen_cld ());
12511       src = replace_equiv_address_nv (src, scratch3);
12512
12513       /* If .md starts supporting :P, this can be done in .md.  */
12514       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
12515                                                  scratch4), UNSPEC_SCAS);
12516       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
12517       if (TARGET_64BIT)
12518         {
12519           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
12520           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
12521         }
12522       else
12523         {
12524           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
12525           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
12526         }
12527     }
12528   return 1;
12529 }
12530
12531 /* Expand the appropriate insns for doing strlen if not just doing
12532    repnz; scasb
12533
12534    out = result, initialized with the start address
12535    align_rtx = alignment of the address.
12536    scratch = scratch register, initialized with the startaddress when
12537         not aligned, otherwise undefined
12538
12539    This is just the body. It needs the initializations mentioned above and
12540    some address computing at the end.  These things are done in i386.md.  */
12541
12542 static void
12543 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
12544 {
12545   int align;
12546   rtx tmp;
12547   rtx align_2_label = NULL_RTX;
12548   rtx align_3_label = NULL_RTX;
12549   rtx align_4_label = gen_label_rtx ();
12550   rtx end_0_label = gen_label_rtx ();
12551   rtx mem;
12552   rtx tmpreg = gen_reg_rtx (SImode);
12553   rtx scratch = gen_reg_rtx (SImode);
12554   rtx cmp;
12555
12556   align = 0;
12557   if (GET_CODE (align_rtx) == CONST_INT)
12558     align = INTVAL (align_rtx);
12559
12560   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
12561
12562   /* Is there a known alignment and is it less than 4?  */
12563   if (align < 4)
12564     {
12565       rtx scratch1 = gen_reg_rtx (Pmode);
12566       emit_move_insn (scratch1, out);
12567       /* Is there a known alignment and is it not 2? */
12568       if (align != 2)
12569         {
12570           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
12571           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
12572
12573           /* Leave just the 3 lower bits.  */
12574           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
12575                                     NULL_RTX, 0, OPTAB_WIDEN);
12576
12577           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
12578                                    Pmode, 1, align_4_label);
12579           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
12580                                    Pmode, 1, align_2_label);
12581           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
12582                                    Pmode, 1, align_3_label);
12583         }
12584       else
12585         {
12586           /* Since the alignment is 2, we have to check 2 or 0 bytes;
12587              check if is aligned to 4 - byte.  */
12588
12589           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
12590                                     NULL_RTX, 0, OPTAB_WIDEN);
12591
12592           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
12593                                    Pmode, 1, align_4_label);
12594         }
12595
12596       mem = change_address (src, QImode, out);
12597
12598       /* Now compare the bytes.  */
12599
12600       /* Compare the first n unaligned byte on a byte per byte basis.  */
12601       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
12602                                QImode, 1, end_0_label);
12603
12604       /* Increment the address.  */
12605       if (TARGET_64BIT)
12606         emit_insn (gen_adddi3 (out, out, const1_rtx));
12607       else
12608         emit_insn (gen_addsi3 (out, out, const1_rtx));
12609
12610       /* Not needed with an alignment of 2 */
12611       if (align != 2)
12612         {
12613           emit_label (align_2_label);
12614
12615           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
12616                                    end_0_label);
12617
12618           if (TARGET_64BIT)
12619             emit_insn (gen_adddi3 (out, out, const1_rtx));
12620           else
12621             emit_insn (gen_addsi3 (out, out, const1_rtx));
12622
12623           emit_label (align_3_label);
12624         }
12625
12626       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
12627                                end_0_label);
12628
12629       if (TARGET_64BIT)
12630         emit_insn (gen_adddi3 (out, out, const1_rtx));
12631       else
12632         emit_insn (gen_addsi3 (out, out, const1_rtx));
12633     }
12634
12635   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
12636      align this loop.  It gives only huge programs, but does not help to
12637      speed up.  */
12638   emit_label (align_4_label);
12639
12640   mem = change_address (src, SImode, out);
12641   emit_move_insn (scratch, mem);
12642   if (TARGET_64BIT)
12643     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
12644   else
12645     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
12646
12647   /* This formula yields a nonzero result iff one of the bytes is zero.
12648      This saves three branches inside loop and many cycles.  */
12649
12650   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
12651   emit_insn (gen_one_cmplsi2 (scratch, scratch));
12652   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
12653   emit_insn (gen_andsi3 (tmpreg, tmpreg,
12654                          gen_int_mode (0x80808080, SImode)));
12655   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
12656                            align_4_label);
12657
12658   if (TARGET_CMOVE)
12659     {
12660        rtx reg = gen_reg_rtx (SImode);
12661        rtx reg2 = gen_reg_rtx (Pmode);
12662        emit_move_insn (reg, tmpreg);
12663        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
12664
12665        /* If zero is not in the first two bytes, move two bytes forward.  */
12666        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
12667        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
12668        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
12669        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
12670                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
12671                                                      reg,
12672                                                      tmpreg)));
12673        /* Emit lea manually to avoid clobbering of flags.  */
12674        emit_insn (gen_rtx_SET (SImode, reg2,
12675                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
12676
12677        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
12678        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
12679        emit_insn (gen_rtx_SET (VOIDmode, out,
12680                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
12681                                                      reg2,
12682                                                      out)));
12683
12684     }
12685   else
12686     {
12687        rtx end_2_label = gen_label_rtx ();
12688        /* Is zero in the first two bytes? */
12689
12690        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
12691        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
12692        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
12693        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
12694                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
12695                             pc_rtx);
12696        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
12697        JUMP_LABEL (tmp) = end_2_label;
12698
12699        /* Not in the first two.  Move two bytes forward.  */
12700        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
12701        if (TARGET_64BIT)
12702          emit_insn (gen_adddi3 (out, out, const2_rtx));
12703        else
12704          emit_insn (gen_addsi3 (out, out, const2_rtx));
12705
12706        emit_label (end_2_label);
12707
12708     }
12709
12710   /* Avoid branch in fixing the byte.  */
12711   tmpreg = gen_lowpart (QImode, tmpreg);
12712   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
12713   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
12714   if (TARGET_64BIT)
12715     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
12716   else
12717     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
12718
12719   emit_label (end_0_label);
12720 }
12721
12722 void
12723 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
12724                   rtx callarg2 ATTRIBUTE_UNUSED,
12725                   rtx pop, int sibcall)
12726 {
12727   rtx use = NULL, call;
12728
12729   if (pop == const0_rtx)
12730     pop = NULL;
12731   gcc_assert (!TARGET_64BIT || !pop);
12732
12733 #if TARGET_MACHO
12734   if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
12735     fnaddr = machopic_indirect_call_target (fnaddr);
12736 #else
12737   /* Static functions and indirect calls don't need the pic register.  */
12738   if (! TARGET_64BIT && flag_pic
12739       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
12740       && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
12741     use_reg (&use, pic_offset_table_rtx);
12742
12743   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
12744     {
12745       rtx al = gen_rtx_REG (QImode, 0);
12746       emit_move_insn (al, callarg2);
12747       use_reg (&use, al);
12748     }
12749 #endif /* TARGET_MACHO */
12750
12751   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
12752     {
12753       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
12754       fnaddr = gen_rtx_MEM (QImode, fnaddr);
12755     }
12756   if (sibcall && TARGET_64BIT
12757       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
12758     {
12759       rtx addr;
12760       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
12761       fnaddr = gen_rtx_REG (Pmode, FIRST_REX_INT_REG + 3 /* R11 */);
12762       emit_move_insn (fnaddr, addr);
12763       fnaddr = gen_rtx_MEM (QImode, fnaddr);
12764     }
12765
12766   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
12767   if (retval)
12768     call = gen_rtx_SET (VOIDmode, retval, call);
12769   if (pop)
12770     {
12771       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
12772       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
12773       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
12774     }
12775
12776   call = emit_call_insn (call);
12777   if (use)
12778     CALL_INSN_FUNCTION_USAGE (call) = use;
12779 }
12780
12781 \f
12782 /* Clear stack slot assignments remembered from previous functions.
12783    This is called from INIT_EXPANDERS once before RTL is emitted for each
12784    function.  */
12785
12786 static struct machine_function *
12787 ix86_init_machine_status (void)
12788 {
12789   struct machine_function *f;
12790
12791   f = ggc_alloc_cleared (sizeof (struct machine_function));
12792   f->use_fast_prologue_epilogue_nregs = -1;
12793
12794   return f;
12795 }
12796
12797 /* Return a MEM corresponding to a stack slot with mode MODE.
12798    Allocate a new slot if necessary.
12799
12800    The RTL for a function can have several slots available: N is
12801    which slot to use.  */
12802
12803 rtx
12804 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
12805 {
12806   struct stack_local_entry *s;
12807
12808   gcc_assert (n < MAX_386_STACK_LOCALS);
12809
12810   for (s = ix86_stack_locals; s; s = s->next)
12811     if (s->mode == mode && s->n == n)
12812       return s->rtl;
12813
12814   s = (struct stack_local_entry *)
12815     ggc_alloc (sizeof (struct stack_local_entry));
12816   s->n = n;
12817   s->mode = mode;
12818   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
12819
12820   s->next = ix86_stack_locals;
12821   ix86_stack_locals = s;
12822   return s->rtl;
12823 }
12824
12825 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
12826
12827 static GTY(()) rtx ix86_tls_symbol;
12828 rtx
12829 ix86_tls_get_addr (void)
12830 {
12831
12832   if (!ix86_tls_symbol)
12833     {
12834       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
12835                                             (TARGET_GNU_TLS && !TARGET_64BIT)
12836                                             ? "___tls_get_addr"
12837                                             : "__tls_get_addr");
12838     }
12839
12840   return ix86_tls_symbol;
12841 }
12842 \f
12843 /* Calculate the length of the memory address in the instruction
12844    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
12845
12846 int
12847 memory_address_length (rtx addr)
12848 {
12849   struct ix86_address parts;
12850   rtx base, index, disp;
12851   int len;
12852   int ok;
12853
12854   if (GET_CODE (addr) == PRE_DEC
12855       || GET_CODE (addr) == POST_INC
12856       || GET_CODE (addr) == PRE_MODIFY
12857       || GET_CODE (addr) == POST_MODIFY)
12858     return 0;
12859
12860   ok = ix86_decompose_address (addr, &parts);
12861   gcc_assert (ok);
12862
12863   if (parts.base && GET_CODE (parts.base) == SUBREG)
12864     parts.base = SUBREG_REG (parts.base);
12865   if (parts.index && GET_CODE (parts.index) == SUBREG)
12866     parts.index = SUBREG_REG (parts.index);
12867
12868   base = parts.base;
12869   index = parts.index;
12870   disp = parts.disp;
12871   len = 0;
12872
12873   /* Rule of thumb:
12874        - esp as the base always wants an index,
12875        - ebp as the base always wants a displacement.  */
12876
12877   /* Register Indirect.  */
12878   if (base && !index && !disp)
12879     {
12880       /* esp (for its index) and ebp (for its displacement) need
12881          the two-byte modrm form.  */
12882       if (addr == stack_pointer_rtx
12883           || addr == arg_pointer_rtx
12884           || addr == frame_pointer_rtx
12885           || addr == hard_frame_pointer_rtx)
12886         len = 1;
12887     }
12888
12889   /* Direct Addressing.  */
12890   else if (disp && !base && !index)
12891     len = 4;
12892
12893   else
12894     {
12895       /* Find the length of the displacement constant.  */
12896       if (disp)
12897         {
12898           if (GET_CODE (disp) == CONST_INT
12899               && CONST_OK_FOR_LETTER_P (INTVAL (disp), 'K')
12900               && base)
12901             len = 1;
12902           else
12903             len = 4;
12904         }
12905       /* ebp always wants a displacement.  */
12906       else if (base == hard_frame_pointer_rtx)
12907         len = 1;
12908
12909       /* An index requires the two-byte modrm form....  */
12910       if (index
12911           /* ...like esp, which always wants an index.  */
12912           || base == stack_pointer_rtx
12913           || base == arg_pointer_rtx
12914           || base == frame_pointer_rtx)
12915         len += 1;
12916     }
12917
12918   return len;
12919 }
12920
12921 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
12922    is set, expect that insn have 8bit immediate alternative.  */
12923 int
12924 ix86_attr_length_immediate_default (rtx insn, int shortform)
12925 {
12926   int len = 0;
12927   int i;
12928   extract_insn_cached (insn);
12929   for (i = recog_data.n_operands - 1; i >= 0; --i)
12930     if (CONSTANT_P (recog_data.operand[i]))
12931       {
12932         gcc_assert (!len);
12933         if (shortform
12934             && GET_CODE (recog_data.operand[i]) == CONST_INT
12935             && CONST_OK_FOR_LETTER_P (INTVAL (recog_data.operand[i]), 'K'))
12936           len = 1;
12937         else
12938           {
12939             switch (get_attr_mode (insn))
12940               {
12941                 case MODE_QI:
12942                   len+=1;
12943                   break;
12944                 case MODE_HI:
12945                   len+=2;
12946                   break;
12947                 case MODE_SI:
12948                   len+=4;
12949                   break;
12950                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
12951                 case MODE_DI:
12952                   len+=4;
12953                   break;
12954                 default:
12955                   fatal_insn ("unknown insn mode", insn);
12956               }
12957           }
12958       }
12959   return len;
12960 }
12961 /* Compute default value for "length_address" attribute.  */
12962 int
12963 ix86_attr_length_address_default (rtx insn)
12964 {
12965   int i;
12966
12967   if (get_attr_type (insn) == TYPE_LEA)
12968     {
12969       rtx set = PATTERN (insn);
12970
12971       if (GET_CODE (set) == PARALLEL)
12972         set = XVECEXP (set, 0, 0);
12973
12974       gcc_assert (GET_CODE (set) == SET);
12975
12976       return memory_address_length (SET_SRC (set));
12977     }
12978
12979   extract_insn_cached (insn);
12980   for (i = recog_data.n_operands - 1; i >= 0; --i)
12981     if (GET_CODE (recog_data.operand[i]) == MEM)
12982       {
12983         return memory_address_length (XEXP (recog_data.operand[i], 0));
12984         break;
12985       }
12986   return 0;
12987 }
12988 \f
12989 /* Return the maximum number of instructions a cpu can issue.  */
12990
12991 static int
12992 ix86_issue_rate (void)
12993 {
12994   switch (ix86_tune)
12995     {
12996     case PROCESSOR_PENTIUM:
12997     case PROCESSOR_K6:
12998       return 2;
12999
13000     case PROCESSOR_PENTIUMPRO:
13001     case PROCESSOR_PENTIUM4:
13002     case PROCESSOR_ATHLON:
13003     case PROCESSOR_K8:
13004     case PROCESSOR_NOCONA:
13005       return 3;
13006
13007     default:
13008       return 1;
13009     }
13010 }
13011
13012 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
13013    by DEP_INSN and nothing set by DEP_INSN.  */
13014
13015 static int
13016 ix86_flags_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
13017 {
13018   rtx set, set2;
13019
13020   /* Simplify the test for uninteresting insns.  */
13021   if (insn_type != TYPE_SETCC
13022       && insn_type != TYPE_ICMOV
13023       && insn_type != TYPE_FCMOV
13024       && insn_type != TYPE_IBR)
13025     return 0;
13026
13027   if ((set = single_set (dep_insn)) != 0)
13028     {
13029       set = SET_DEST (set);
13030       set2 = NULL_RTX;
13031     }
13032   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
13033            && XVECLEN (PATTERN (dep_insn), 0) == 2
13034            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
13035            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
13036     {
13037       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13038       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13039     }
13040   else
13041     return 0;
13042
13043   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
13044     return 0;
13045
13046   /* This test is true if the dependent insn reads the flags but
13047      not any other potentially set register.  */
13048   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
13049     return 0;
13050
13051   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
13052     return 0;
13053
13054   return 1;
13055 }
13056
13057 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
13058    address with operands set by DEP_INSN.  */
13059
13060 static int
13061 ix86_agi_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
13062 {
13063   rtx addr;
13064
13065   if (insn_type == TYPE_LEA
13066       && TARGET_PENTIUM)
13067     {
13068       addr = PATTERN (insn);
13069
13070       if (GET_CODE (addr) == PARALLEL)
13071         addr = XVECEXP (addr, 0, 0);
13072       
13073       gcc_assert (GET_CODE (addr) == SET);
13074       
13075       addr = SET_SRC (addr);
13076     }
13077   else
13078     {
13079       int i;
13080       extract_insn_cached (insn);
13081       for (i = recog_data.n_operands - 1; i >= 0; --i)
13082         if (GET_CODE (recog_data.operand[i]) == MEM)
13083           {
13084             addr = XEXP (recog_data.operand[i], 0);
13085             goto found;
13086           }
13087       return 0;
13088     found:;
13089     }
13090
13091   return modified_in_p (addr, dep_insn);
13092 }
13093
13094 static int
13095 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
13096 {
13097   enum attr_type insn_type, dep_insn_type;
13098   enum attr_memory memory;
13099   rtx set, set2;
13100   int dep_insn_code_number;
13101
13102   /* Anti and output dependencies have zero cost on all CPUs.  */
13103   if (REG_NOTE_KIND (link) != 0)
13104     return 0;
13105
13106   dep_insn_code_number = recog_memoized (dep_insn);
13107
13108   /* If we can't recognize the insns, we can't really do anything.  */
13109   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
13110     return cost;
13111
13112   insn_type = get_attr_type (insn);
13113   dep_insn_type = get_attr_type (dep_insn);
13114
13115   switch (ix86_tune)
13116     {
13117     case PROCESSOR_PENTIUM:
13118       /* Address Generation Interlock adds a cycle of latency.  */
13119       if (ix86_agi_dependant (insn, dep_insn, insn_type))
13120         cost += 1;
13121
13122       /* ??? Compares pair with jump/setcc.  */
13123       if (ix86_flags_dependant (insn, dep_insn, insn_type))
13124         cost = 0;
13125
13126       /* Floating point stores require value to be ready one cycle earlier.  */
13127       if (insn_type == TYPE_FMOV
13128           && get_attr_memory (insn) == MEMORY_STORE
13129           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13130         cost += 1;
13131       break;
13132
13133     case PROCESSOR_PENTIUMPRO:
13134       memory = get_attr_memory (insn);
13135
13136       /* INT->FP conversion is expensive.  */
13137       if (get_attr_fp_int_src (dep_insn))
13138         cost += 5;
13139
13140       /* There is one cycle extra latency between an FP op and a store.  */
13141       if (insn_type == TYPE_FMOV
13142           && (set = single_set (dep_insn)) != NULL_RTX
13143           && (set2 = single_set (insn)) != NULL_RTX
13144           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
13145           && GET_CODE (SET_DEST (set2)) == MEM)
13146         cost += 1;
13147
13148       /* Show ability of reorder buffer to hide latency of load by executing
13149          in parallel with previous instruction in case
13150          previous instruction is not needed to compute the address.  */
13151       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13152           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13153         {
13154           /* Claim moves to take one cycle, as core can issue one load
13155              at time and the next load can start cycle later.  */
13156           if (dep_insn_type == TYPE_IMOV
13157               || dep_insn_type == TYPE_FMOV)
13158             cost = 1;
13159           else if (cost > 1)
13160             cost--;
13161         }
13162       break;
13163
13164     case PROCESSOR_K6:
13165       memory = get_attr_memory (insn);
13166
13167       /* The esp dependency is resolved before the instruction is really
13168          finished.  */
13169       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
13170           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
13171         return 1;
13172
13173       /* INT->FP conversion is expensive.  */
13174       if (get_attr_fp_int_src (dep_insn))
13175         cost += 5;
13176
13177       /* Show ability of reorder buffer to hide latency of load by executing
13178          in parallel with previous instruction in case
13179          previous instruction is not needed to compute the address.  */
13180       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13181           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13182         {
13183           /* Claim moves to take one cycle, as core can issue one load
13184              at time and the next load can start cycle later.  */
13185           if (dep_insn_type == TYPE_IMOV
13186               || dep_insn_type == TYPE_FMOV)
13187             cost = 1;
13188           else if (cost > 2)
13189             cost -= 2;
13190           else
13191             cost = 1;
13192         }
13193       break;
13194
13195     case PROCESSOR_ATHLON:
13196     case PROCESSOR_K8:
13197       memory = get_attr_memory (insn);
13198
13199       /* Show ability of reorder buffer to hide latency of load by executing
13200          in parallel with previous instruction in case
13201          previous instruction is not needed to compute the address.  */
13202       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13203           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13204         {
13205           enum attr_unit unit = get_attr_unit (insn);
13206           int loadcost = 3;
13207
13208           /* Because of the difference between the length of integer and
13209              floating unit pipeline preparation stages, the memory operands
13210              for floating point are cheaper.
13211
13212              ??? For Athlon it the difference is most probably 2.  */
13213           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
13214             loadcost = 3;
13215           else
13216             loadcost = TARGET_ATHLON ? 2 : 0;
13217
13218           if (cost >= loadcost)
13219             cost -= loadcost;
13220           else
13221             cost = 0;
13222         }
13223
13224     default:
13225       break;
13226     }
13227
13228   return cost;
13229 }
13230
13231 /* How many alternative schedules to try.  This should be as wide as the
13232    scheduling freedom in the DFA, but no wider.  Making this value too
13233    large results extra work for the scheduler.  */
13234
13235 static int
13236 ia32_multipass_dfa_lookahead (void)
13237 {
13238   if (ix86_tune == PROCESSOR_PENTIUM)
13239     return 2;
13240
13241   if (ix86_tune == PROCESSOR_PENTIUMPRO
13242       || ix86_tune == PROCESSOR_K6)
13243     return 1;
13244
13245   else
13246     return 0;
13247 }
13248
13249 \f
13250 /* Compute the alignment given to a constant that is being placed in memory.
13251    EXP is the constant and ALIGN is the alignment that the object would
13252    ordinarily have.
13253    The value of this function is used instead of that alignment to align
13254    the object.  */
13255
13256 int
13257 ix86_constant_alignment (tree exp, int align)
13258 {
13259   if (TREE_CODE (exp) == REAL_CST)
13260     {
13261       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
13262         return 64;
13263       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
13264         return 128;
13265     }
13266   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
13267            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
13268     return BITS_PER_WORD;
13269
13270   return align;
13271 }
13272
13273 /* Compute the alignment for a static variable.
13274    TYPE is the data type, and ALIGN is the alignment that
13275    the object would ordinarily have.  The value of this function is used
13276    instead of that alignment to align the object.  */
13277
13278 int
13279 ix86_data_alignment (tree type, int align)
13280 {
13281   if (AGGREGATE_TYPE_P (type)
13282        && TYPE_SIZE (type)
13283        && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13284        && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 256
13285            || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 256)
13286     return 256;
13287
13288   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
13289      to 16byte boundary.  */
13290   if (TARGET_64BIT)
13291     {
13292       if (AGGREGATE_TYPE_P (type)
13293            && TYPE_SIZE (type)
13294            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13295            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
13296                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
13297         return 128;
13298     }
13299
13300   if (TREE_CODE (type) == ARRAY_TYPE)
13301     {
13302       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
13303         return 64;
13304       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
13305         return 128;
13306     }
13307   else if (TREE_CODE (type) == COMPLEX_TYPE)
13308     {
13309
13310       if (TYPE_MODE (type) == DCmode && align < 64)
13311         return 64;
13312       if (TYPE_MODE (type) == XCmode && align < 128)
13313         return 128;
13314     }
13315   else if ((TREE_CODE (type) == RECORD_TYPE
13316             || TREE_CODE (type) == UNION_TYPE
13317             || TREE_CODE (type) == QUAL_UNION_TYPE)
13318            && TYPE_FIELDS (type))
13319     {
13320       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
13321         return 64;
13322       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
13323         return 128;
13324     }
13325   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
13326            || TREE_CODE (type) == INTEGER_TYPE)
13327     {
13328       if (TYPE_MODE (type) == DFmode && align < 64)
13329         return 64;
13330       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
13331         return 128;
13332     }
13333
13334   return align;
13335 }
13336
13337 /* Compute the alignment for a local variable.
13338    TYPE is the data type, and ALIGN is the alignment that
13339    the object would ordinarily have.  The value of this macro is used
13340    instead of that alignment to align the object.  */
13341
13342 int
13343 ix86_local_alignment (tree type, int align)
13344 {
13345   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
13346      to 16byte boundary.  */
13347   if (TARGET_64BIT)
13348     {
13349       if (AGGREGATE_TYPE_P (type)
13350            && TYPE_SIZE (type)
13351            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13352            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
13353                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
13354         return 128;
13355     }
13356   if (TREE_CODE (type) == ARRAY_TYPE)
13357     {
13358       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
13359         return 64;
13360       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
13361         return 128;
13362     }
13363   else if (TREE_CODE (type) == COMPLEX_TYPE)
13364     {
13365       if (TYPE_MODE (type) == DCmode && align < 64)
13366         return 64;
13367       if (TYPE_MODE (type) == XCmode && align < 128)
13368         return 128;
13369     }
13370   else if ((TREE_CODE (type) == RECORD_TYPE
13371             || TREE_CODE (type) == UNION_TYPE
13372             || TREE_CODE (type) == QUAL_UNION_TYPE)
13373            && TYPE_FIELDS (type))
13374     {
13375       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
13376         return 64;
13377       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
13378         return 128;
13379     }
13380   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
13381            || TREE_CODE (type) == INTEGER_TYPE)
13382     {
13383
13384       if (TYPE_MODE (type) == DFmode && align < 64)
13385         return 64;
13386       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
13387         return 128;
13388     }
13389   return align;
13390 }
13391 \f
13392 /* Emit RTL insns to initialize the variable parts of a trampoline.
13393    FNADDR is an RTX for the address of the function's pure code.
13394    CXT is an RTX for the static chain value for the function.  */
13395 void
13396 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
13397 {
13398   if (!TARGET_64BIT)
13399     {
13400       /* Compute offset from the end of the jmp to the target function.  */
13401       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
13402                                plus_constant (tramp, 10),
13403                                NULL_RTX, 1, OPTAB_DIRECT);
13404       emit_move_insn (gen_rtx_MEM (QImode, tramp),
13405                       gen_int_mode (0xb9, QImode));
13406       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
13407       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
13408                       gen_int_mode (0xe9, QImode));
13409       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
13410     }
13411   else
13412     {
13413       int offset = 0;
13414       /* Try to load address using shorter movl instead of movabs.
13415          We may want to support movq for kernel mode, but kernel does not use
13416          trampolines at the moment.  */
13417       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
13418         {
13419           fnaddr = copy_to_mode_reg (DImode, fnaddr);
13420           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13421                           gen_int_mode (0xbb41, HImode));
13422           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
13423                           gen_lowpart (SImode, fnaddr));
13424           offset += 6;
13425         }
13426       else
13427         {
13428           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13429                           gen_int_mode (0xbb49, HImode));
13430           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
13431                           fnaddr);
13432           offset += 10;
13433         }
13434       /* Load static chain using movabs to r10.  */
13435       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13436                       gen_int_mode (0xba49, HImode));
13437       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
13438                       cxt);
13439       offset += 10;
13440       /* Jump to the r11 */
13441       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13442                       gen_int_mode (0xff49, HImode));
13443       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
13444                       gen_int_mode (0xe3, QImode));
13445       offset += 3;
13446       gcc_assert (offset <= TRAMPOLINE_SIZE);
13447     }
13448
13449 #ifdef ENABLE_EXECUTE_STACK
13450   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
13451                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
13452 #endif
13453 }
13454 \f
13455 /* Codes for all the SSE/MMX builtins.  */
13456 enum ix86_builtins
13457 {
13458   IX86_BUILTIN_ADDPS,
13459   IX86_BUILTIN_ADDSS,
13460   IX86_BUILTIN_DIVPS,
13461   IX86_BUILTIN_DIVSS,
13462   IX86_BUILTIN_MULPS,
13463   IX86_BUILTIN_MULSS,
13464   IX86_BUILTIN_SUBPS,
13465   IX86_BUILTIN_SUBSS,
13466
13467   IX86_BUILTIN_CMPEQPS,
13468   IX86_BUILTIN_CMPLTPS,
13469   IX86_BUILTIN_CMPLEPS,
13470   IX86_BUILTIN_CMPGTPS,
13471   IX86_BUILTIN_CMPGEPS,
13472   IX86_BUILTIN_CMPNEQPS,
13473   IX86_BUILTIN_CMPNLTPS,
13474   IX86_BUILTIN_CMPNLEPS,
13475   IX86_BUILTIN_CMPNGTPS,
13476   IX86_BUILTIN_CMPNGEPS,
13477   IX86_BUILTIN_CMPORDPS,
13478   IX86_BUILTIN_CMPUNORDPS,
13479   IX86_BUILTIN_CMPEQSS,
13480   IX86_BUILTIN_CMPLTSS,
13481   IX86_BUILTIN_CMPLESS,
13482   IX86_BUILTIN_CMPNEQSS,
13483   IX86_BUILTIN_CMPNLTSS,
13484   IX86_BUILTIN_CMPNLESS,
13485   IX86_BUILTIN_CMPNGTSS,
13486   IX86_BUILTIN_CMPNGESS,
13487   IX86_BUILTIN_CMPORDSS,
13488   IX86_BUILTIN_CMPUNORDSS,
13489
13490   IX86_BUILTIN_COMIEQSS,
13491   IX86_BUILTIN_COMILTSS,
13492   IX86_BUILTIN_COMILESS,
13493   IX86_BUILTIN_COMIGTSS,
13494   IX86_BUILTIN_COMIGESS,
13495   IX86_BUILTIN_COMINEQSS,
13496   IX86_BUILTIN_UCOMIEQSS,
13497   IX86_BUILTIN_UCOMILTSS,
13498   IX86_BUILTIN_UCOMILESS,
13499   IX86_BUILTIN_UCOMIGTSS,
13500   IX86_BUILTIN_UCOMIGESS,
13501   IX86_BUILTIN_UCOMINEQSS,
13502
13503   IX86_BUILTIN_CVTPI2PS,
13504   IX86_BUILTIN_CVTPS2PI,
13505   IX86_BUILTIN_CVTSI2SS,
13506   IX86_BUILTIN_CVTSI642SS,
13507   IX86_BUILTIN_CVTSS2SI,
13508   IX86_BUILTIN_CVTSS2SI64,
13509   IX86_BUILTIN_CVTTPS2PI,
13510   IX86_BUILTIN_CVTTSS2SI,
13511   IX86_BUILTIN_CVTTSS2SI64,
13512
13513   IX86_BUILTIN_MAXPS,
13514   IX86_BUILTIN_MAXSS,
13515   IX86_BUILTIN_MINPS,
13516   IX86_BUILTIN_MINSS,
13517
13518   IX86_BUILTIN_LOADUPS,
13519   IX86_BUILTIN_STOREUPS,
13520   IX86_BUILTIN_MOVSS,
13521
13522   IX86_BUILTIN_MOVHLPS,
13523   IX86_BUILTIN_MOVLHPS,
13524   IX86_BUILTIN_LOADHPS,
13525   IX86_BUILTIN_LOADLPS,
13526   IX86_BUILTIN_STOREHPS,
13527   IX86_BUILTIN_STORELPS,
13528
13529   IX86_BUILTIN_MASKMOVQ,
13530   IX86_BUILTIN_MOVMSKPS,
13531   IX86_BUILTIN_PMOVMSKB,
13532
13533   IX86_BUILTIN_MOVNTPS,
13534   IX86_BUILTIN_MOVNTQ,
13535
13536   IX86_BUILTIN_LOADDQU,
13537   IX86_BUILTIN_STOREDQU,
13538
13539   IX86_BUILTIN_PACKSSWB,
13540   IX86_BUILTIN_PACKSSDW,
13541   IX86_BUILTIN_PACKUSWB,
13542
13543   IX86_BUILTIN_PADDB,
13544   IX86_BUILTIN_PADDW,
13545   IX86_BUILTIN_PADDD,
13546   IX86_BUILTIN_PADDQ,
13547   IX86_BUILTIN_PADDSB,
13548   IX86_BUILTIN_PADDSW,
13549   IX86_BUILTIN_PADDUSB,
13550   IX86_BUILTIN_PADDUSW,
13551   IX86_BUILTIN_PSUBB,
13552   IX86_BUILTIN_PSUBW,
13553   IX86_BUILTIN_PSUBD,
13554   IX86_BUILTIN_PSUBQ,
13555   IX86_BUILTIN_PSUBSB,
13556   IX86_BUILTIN_PSUBSW,
13557   IX86_BUILTIN_PSUBUSB,
13558   IX86_BUILTIN_PSUBUSW,
13559
13560   IX86_BUILTIN_PAND,
13561   IX86_BUILTIN_PANDN,
13562   IX86_BUILTIN_POR,
13563   IX86_BUILTIN_PXOR,
13564
13565   IX86_BUILTIN_PAVGB,
13566   IX86_BUILTIN_PAVGW,
13567
13568   IX86_BUILTIN_PCMPEQB,
13569   IX86_BUILTIN_PCMPEQW,
13570   IX86_BUILTIN_PCMPEQD,
13571   IX86_BUILTIN_PCMPGTB,
13572   IX86_BUILTIN_PCMPGTW,
13573   IX86_BUILTIN_PCMPGTD,
13574
13575   IX86_BUILTIN_PMADDWD,
13576
13577   IX86_BUILTIN_PMAXSW,
13578   IX86_BUILTIN_PMAXUB,
13579   IX86_BUILTIN_PMINSW,
13580   IX86_BUILTIN_PMINUB,
13581
13582   IX86_BUILTIN_PMULHUW,
13583   IX86_BUILTIN_PMULHW,
13584   IX86_BUILTIN_PMULLW,
13585
13586   IX86_BUILTIN_PSADBW,
13587   IX86_BUILTIN_PSHUFW,
13588
13589   IX86_BUILTIN_PSLLW,
13590   IX86_BUILTIN_PSLLD,
13591   IX86_BUILTIN_PSLLQ,
13592   IX86_BUILTIN_PSRAW,
13593   IX86_BUILTIN_PSRAD,
13594   IX86_BUILTIN_PSRLW,
13595   IX86_BUILTIN_PSRLD,
13596   IX86_BUILTIN_PSRLQ,
13597   IX86_BUILTIN_PSLLWI,
13598   IX86_BUILTIN_PSLLDI,
13599   IX86_BUILTIN_PSLLQI,
13600   IX86_BUILTIN_PSRAWI,
13601   IX86_BUILTIN_PSRADI,
13602   IX86_BUILTIN_PSRLWI,
13603   IX86_BUILTIN_PSRLDI,
13604   IX86_BUILTIN_PSRLQI,
13605
13606   IX86_BUILTIN_PUNPCKHBW,
13607   IX86_BUILTIN_PUNPCKHWD,
13608   IX86_BUILTIN_PUNPCKHDQ,
13609   IX86_BUILTIN_PUNPCKLBW,
13610   IX86_BUILTIN_PUNPCKLWD,
13611   IX86_BUILTIN_PUNPCKLDQ,
13612
13613   IX86_BUILTIN_SHUFPS,
13614
13615   IX86_BUILTIN_RCPPS,
13616   IX86_BUILTIN_RCPSS,
13617   IX86_BUILTIN_RSQRTPS,
13618   IX86_BUILTIN_RSQRTSS,
13619   IX86_BUILTIN_SQRTPS,
13620   IX86_BUILTIN_SQRTSS,
13621
13622   IX86_BUILTIN_UNPCKHPS,
13623   IX86_BUILTIN_UNPCKLPS,
13624
13625   IX86_BUILTIN_ANDPS,
13626   IX86_BUILTIN_ANDNPS,
13627   IX86_BUILTIN_ORPS,
13628   IX86_BUILTIN_XORPS,
13629
13630   IX86_BUILTIN_EMMS,
13631   IX86_BUILTIN_LDMXCSR,
13632   IX86_BUILTIN_STMXCSR,
13633   IX86_BUILTIN_SFENCE,
13634
13635   /* 3DNow! Original */
13636   IX86_BUILTIN_FEMMS,
13637   IX86_BUILTIN_PAVGUSB,
13638   IX86_BUILTIN_PF2ID,
13639   IX86_BUILTIN_PFACC,
13640   IX86_BUILTIN_PFADD,
13641   IX86_BUILTIN_PFCMPEQ,
13642   IX86_BUILTIN_PFCMPGE,
13643   IX86_BUILTIN_PFCMPGT,
13644   IX86_BUILTIN_PFMAX,
13645   IX86_BUILTIN_PFMIN,
13646   IX86_BUILTIN_PFMUL,
13647   IX86_BUILTIN_PFRCP,
13648   IX86_BUILTIN_PFRCPIT1,
13649   IX86_BUILTIN_PFRCPIT2,
13650   IX86_BUILTIN_PFRSQIT1,
13651   IX86_BUILTIN_PFRSQRT,
13652   IX86_BUILTIN_PFSUB,
13653   IX86_BUILTIN_PFSUBR,
13654   IX86_BUILTIN_PI2FD,
13655   IX86_BUILTIN_PMULHRW,
13656
13657   /* 3DNow! Athlon Extensions */
13658   IX86_BUILTIN_PF2IW,
13659   IX86_BUILTIN_PFNACC,
13660   IX86_BUILTIN_PFPNACC,
13661   IX86_BUILTIN_PI2FW,
13662   IX86_BUILTIN_PSWAPDSI,
13663   IX86_BUILTIN_PSWAPDSF,
13664
13665   /* SSE2 */
13666   IX86_BUILTIN_ADDPD,
13667   IX86_BUILTIN_ADDSD,
13668   IX86_BUILTIN_DIVPD,
13669   IX86_BUILTIN_DIVSD,
13670   IX86_BUILTIN_MULPD,
13671   IX86_BUILTIN_MULSD,
13672   IX86_BUILTIN_SUBPD,
13673   IX86_BUILTIN_SUBSD,
13674
13675   IX86_BUILTIN_CMPEQPD,
13676   IX86_BUILTIN_CMPLTPD,
13677   IX86_BUILTIN_CMPLEPD,
13678   IX86_BUILTIN_CMPGTPD,
13679   IX86_BUILTIN_CMPGEPD,
13680   IX86_BUILTIN_CMPNEQPD,
13681   IX86_BUILTIN_CMPNLTPD,
13682   IX86_BUILTIN_CMPNLEPD,
13683   IX86_BUILTIN_CMPNGTPD,
13684   IX86_BUILTIN_CMPNGEPD,
13685   IX86_BUILTIN_CMPORDPD,
13686   IX86_BUILTIN_CMPUNORDPD,
13687   IX86_BUILTIN_CMPNEPD,
13688   IX86_BUILTIN_CMPEQSD,
13689   IX86_BUILTIN_CMPLTSD,
13690   IX86_BUILTIN_CMPLESD,
13691   IX86_BUILTIN_CMPNEQSD,
13692   IX86_BUILTIN_CMPNLTSD,
13693   IX86_BUILTIN_CMPNLESD,
13694   IX86_BUILTIN_CMPORDSD,
13695   IX86_BUILTIN_CMPUNORDSD,
13696   IX86_BUILTIN_CMPNESD,
13697
13698   IX86_BUILTIN_COMIEQSD,
13699   IX86_BUILTIN_COMILTSD,
13700   IX86_BUILTIN_COMILESD,
13701   IX86_BUILTIN_COMIGTSD,
13702   IX86_BUILTIN_COMIGESD,
13703   IX86_BUILTIN_COMINEQSD,
13704   IX86_BUILTIN_UCOMIEQSD,
13705   IX86_BUILTIN_UCOMILTSD,
13706   IX86_BUILTIN_UCOMILESD,
13707   IX86_BUILTIN_UCOMIGTSD,
13708   IX86_BUILTIN_UCOMIGESD,
13709   IX86_BUILTIN_UCOMINEQSD,
13710
13711   IX86_BUILTIN_MAXPD,
13712   IX86_BUILTIN_MAXSD,
13713   IX86_BUILTIN_MINPD,
13714   IX86_BUILTIN_MINSD,
13715
13716   IX86_BUILTIN_ANDPD,
13717   IX86_BUILTIN_ANDNPD,
13718   IX86_BUILTIN_ORPD,
13719   IX86_BUILTIN_XORPD,
13720
13721   IX86_BUILTIN_SQRTPD,
13722   IX86_BUILTIN_SQRTSD,
13723
13724   IX86_BUILTIN_UNPCKHPD,
13725   IX86_BUILTIN_UNPCKLPD,
13726
13727   IX86_BUILTIN_SHUFPD,
13728
13729   IX86_BUILTIN_LOADUPD,
13730   IX86_BUILTIN_STOREUPD,
13731   IX86_BUILTIN_MOVSD,
13732
13733   IX86_BUILTIN_LOADHPD,
13734   IX86_BUILTIN_LOADLPD,
13735
13736   IX86_BUILTIN_CVTDQ2PD,
13737   IX86_BUILTIN_CVTDQ2PS,
13738
13739   IX86_BUILTIN_CVTPD2DQ,
13740   IX86_BUILTIN_CVTPD2PI,
13741   IX86_BUILTIN_CVTPD2PS,
13742   IX86_BUILTIN_CVTTPD2DQ,
13743   IX86_BUILTIN_CVTTPD2PI,
13744
13745   IX86_BUILTIN_CVTPI2PD,
13746   IX86_BUILTIN_CVTSI2SD,
13747   IX86_BUILTIN_CVTSI642SD,
13748
13749   IX86_BUILTIN_CVTSD2SI,
13750   IX86_BUILTIN_CVTSD2SI64,
13751   IX86_BUILTIN_CVTSD2SS,
13752   IX86_BUILTIN_CVTSS2SD,
13753   IX86_BUILTIN_CVTTSD2SI,
13754   IX86_BUILTIN_CVTTSD2SI64,
13755
13756   IX86_BUILTIN_CVTPS2DQ,
13757   IX86_BUILTIN_CVTPS2PD,
13758   IX86_BUILTIN_CVTTPS2DQ,
13759
13760   IX86_BUILTIN_MOVNTI,
13761   IX86_BUILTIN_MOVNTPD,
13762   IX86_BUILTIN_MOVNTDQ,
13763
13764   /* SSE2 MMX */
13765   IX86_BUILTIN_MASKMOVDQU,
13766   IX86_BUILTIN_MOVMSKPD,
13767   IX86_BUILTIN_PMOVMSKB128,
13768
13769   IX86_BUILTIN_PACKSSWB128,
13770   IX86_BUILTIN_PACKSSDW128,
13771   IX86_BUILTIN_PACKUSWB128,
13772
13773   IX86_BUILTIN_PADDB128,
13774   IX86_BUILTIN_PADDW128,
13775   IX86_BUILTIN_PADDD128,
13776   IX86_BUILTIN_PADDQ128,
13777   IX86_BUILTIN_PADDSB128,
13778   IX86_BUILTIN_PADDSW128,
13779   IX86_BUILTIN_PADDUSB128,
13780   IX86_BUILTIN_PADDUSW128,
13781   IX86_BUILTIN_PSUBB128,
13782   IX86_BUILTIN_PSUBW128,
13783   IX86_BUILTIN_PSUBD128,
13784   IX86_BUILTIN_PSUBQ128,
13785   IX86_BUILTIN_PSUBSB128,
13786   IX86_BUILTIN_PSUBSW128,
13787   IX86_BUILTIN_PSUBUSB128,
13788   IX86_BUILTIN_PSUBUSW128,
13789
13790   IX86_BUILTIN_PAND128,
13791   IX86_BUILTIN_PANDN128,
13792   IX86_BUILTIN_POR128,
13793   IX86_BUILTIN_PXOR128,
13794
13795   IX86_BUILTIN_PAVGB128,
13796   IX86_BUILTIN_PAVGW128,
13797
13798   IX86_BUILTIN_PCMPEQB128,
13799   IX86_BUILTIN_PCMPEQW128,
13800   IX86_BUILTIN_PCMPEQD128,
13801   IX86_BUILTIN_PCMPGTB128,
13802   IX86_BUILTIN_PCMPGTW128,
13803   IX86_BUILTIN_PCMPGTD128,
13804
13805   IX86_BUILTIN_PMADDWD128,
13806
13807   IX86_BUILTIN_PMAXSW128,
13808   IX86_BUILTIN_PMAXUB128,
13809   IX86_BUILTIN_PMINSW128,
13810   IX86_BUILTIN_PMINUB128,
13811
13812   IX86_BUILTIN_PMULUDQ,
13813   IX86_BUILTIN_PMULUDQ128,
13814   IX86_BUILTIN_PMULHUW128,
13815   IX86_BUILTIN_PMULHW128,
13816   IX86_BUILTIN_PMULLW128,
13817
13818   IX86_BUILTIN_PSADBW128,
13819   IX86_BUILTIN_PSHUFHW,
13820   IX86_BUILTIN_PSHUFLW,
13821   IX86_BUILTIN_PSHUFD,
13822
13823   IX86_BUILTIN_PSLLW128,
13824   IX86_BUILTIN_PSLLD128,
13825   IX86_BUILTIN_PSLLQ128,
13826   IX86_BUILTIN_PSRAW128,
13827   IX86_BUILTIN_PSRAD128,
13828   IX86_BUILTIN_PSRLW128,
13829   IX86_BUILTIN_PSRLD128,
13830   IX86_BUILTIN_PSRLQ128,
13831   IX86_BUILTIN_PSLLDQI128,
13832   IX86_BUILTIN_PSLLWI128,
13833   IX86_BUILTIN_PSLLDI128,
13834   IX86_BUILTIN_PSLLQI128,
13835   IX86_BUILTIN_PSRAWI128,
13836   IX86_BUILTIN_PSRADI128,
13837   IX86_BUILTIN_PSRLDQI128,
13838   IX86_BUILTIN_PSRLWI128,
13839   IX86_BUILTIN_PSRLDI128,
13840   IX86_BUILTIN_PSRLQI128,
13841
13842   IX86_BUILTIN_PUNPCKHBW128,
13843   IX86_BUILTIN_PUNPCKHWD128,
13844   IX86_BUILTIN_PUNPCKHDQ128,
13845   IX86_BUILTIN_PUNPCKHQDQ128,
13846   IX86_BUILTIN_PUNPCKLBW128,
13847   IX86_BUILTIN_PUNPCKLWD128,
13848   IX86_BUILTIN_PUNPCKLDQ128,
13849   IX86_BUILTIN_PUNPCKLQDQ128,
13850
13851   IX86_BUILTIN_CLFLUSH,
13852   IX86_BUILTIN_MFENCE,
13853   IX86_BUILTIN_LFENCE,
13854
13855   /* Prescott New Instructions.  */
13856   IX86_BUILTIN_ADDSUBPS,
13857   IX86_BUILTIN_HADDPS,
13858   IX86_BUILTIN_HSUBPS,
13859   IX86_BUILTIN_MOVSHDUP,
13860   IX86_BUILTIN_MOVSLDUP,
13861   IX86_BUILTIN_ADDSUBPD,
13862   IX86_BUILTIN_HADDPD,
13863   IX86_BUILTIN_HSUBPD,
13864   IX86_BUILTIN_LDDQU,
13865
13866   IX86_BUILTIN_MONITOR,
13867   IX86_BUILTIN_MWAIT,
13868
13869   IX86_BUILTIN_VEC_INIT_V2SI,
13870   IX86_BUILTIN_VEC_INIT_V4HI,
13871   IX86_BUILTIN_VEC_INIT_V8QI,
13872   IX86_BUILTIN_VEC_EXT_V2DF,
13873   IX86_BUILTIN_VEC_EXT_V2DI,
13874   IX86_BUILTIN_VEC_EXT_V4SF,
13875   IX86_BUILTIN_VEC_EXT_V4SI,
13876   IX86_BUILTIN_VEC_EXT_V8HI,
13877   IX86_BUILTIN_VEC_EXT_V2SI,
13878   IX86_BUILTIN_VEC_EXT_V4HI,
13879   IX86_BUILTIN_VEC_SET_V8HI,
13880   IX86_BUILTIN_VEC_SET_V4HI,
13881
13882   IX86_BUILTIN_MAX
13883 };
13884
13885 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
13886 do {                                                                    \
13887   if ((MASK) & target_flags                                             \
13888       && (!((MASK) & MASK_64BIT) || TARGET_64BIT))                      \
13889     lang_hooks.builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,   \
13890                                  NULL, NULL_TREE);                      \
13891 } while (0)
13892
13893 /* Bits for builtin_description.flag.  */
13894
13895 /* Set when we don't support the comparison natively, and should
13896    swap_comparison in order to support it.  */
13897 #define BUILTIN_DESC_SWAP_OPERANDS      1
13898
13899 struct builtin_description
13900 {
13901   const unsigned int mask;
13902   const enum insn_code icode;
13903   const char *const name;
13904   const enum ix86_builtins code;
13905   const enum rtx_code comparison;
13906   const unsigned int flag;
13907 };
13908
13909 static const struct builtin_description bdesc_comi[] =
13910 {
13911   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
13912   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
13913   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
13914   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
13915   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
13916   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
13917   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
13918   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
13919   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
13920   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
13921   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
13922   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
13923   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
13924   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
13925   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
13926   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
13927   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
13928   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
13929   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
13930   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
13931   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
13932   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
13933   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
13934   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
13935 };
13936
13937 static const struct builtin_description bdesc_2arg[] =
13938 {
13939   /* SSE */
13940   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
13941   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
13942   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
13943   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
13944   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
13945   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
13946   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
13947   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
13948
13949   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
13950   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
13951   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
13952   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
13953     BUILTIN_DESC_SWAP_OPERANDS },
13954   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
13955     BUILTIN_DESC_SWAP_OPERANDS },
13956   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
13957   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
13958   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
13959   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
13960   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
13961     BUILTIN_DESC_SWAP_OPERANDS },
13962   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
13963     BUILTIN_DESC_SWAP_OPERANDS },
13964   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
13965   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
13966   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
13967   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
13968   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
13969   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
13970   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
13971   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
13972   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
13973     BUILTIN_DESC_SWAP_OPERANDS },
13974   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
13975     BUILTIN_DESC_SWAP_OPERANDS },
13976   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
13977
13978   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
13979   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
13980   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
13981   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
13982
13983   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
13984   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
13985   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
13986   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
13987
13988   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
13989   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
13990   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
13991   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
13992   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
13993
13994   /* MMX */
13995   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
13996   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
13997   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
13998   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
13999   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
14000   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
14001   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
14002   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
14003
14004   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
14005   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
14006   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
14007   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
14008   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
14009   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
14010   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
14011   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
14012
14013   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
14014   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
14015   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
14016
14017   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
14018   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
14019   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
14020   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
14021
14022   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
14023   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
14024
14025   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
14026   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
14027   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
14028   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
14029   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
14030   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
14031
14032   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
14033   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
14034   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
14035   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
14036
14037   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
14038   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
14039   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
14040   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
14041   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
14042   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
14043
14044   /* Special.  */
14045   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
14046   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
14047   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
14048
14049   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
14050   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
14051   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
14052
14053   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
14054   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
14055   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
14056   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
14057   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
14058   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
14059
14060   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
14061   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
14062   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
14063   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
14064   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
14065   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
14066
14067   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
14068   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
14069   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
14070   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
14071
14072   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
14073   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
14074
14075   /* SSE2 */
14076   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
14077   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
14078   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
14079   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
14080   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
14081   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
14082   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
14083   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
14084
14085   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
14086   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
14087   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
14088   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
14089     BUILTIN_DESC_SWAP_OPERANDS },
14090   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
14091     BUILTIN_DESC_SWAP_OPERANDS },
14092   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
14093   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
14094   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
14095   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
14096   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
14097     BUILTIN_DESC_SWAP_OPERANDS },
14098   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
14099     BUILTIN_DESC_SWAP_OPERANDS },
14100   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
14101   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
14102   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
14103   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
14104   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
14105   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
14106   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
14107   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
14108   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
14109
14110   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
14111   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
14112   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
14113   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
14114
14115   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
14116   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
14117   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
14118   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
14119
14120   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
14121   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
14122   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
14123
14124   /* SSE2 MMX */
14125   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
14126   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
14127   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
14128   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
14129   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
14130   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
14131   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
14132   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
14133
14134   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
14135   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
14136   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
14137   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
14138   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
14139   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
14140   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
14141   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
14142
14143   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
14144   { MASK_SSE2, CODE_FOR_sse2_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
14145
14146   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
14147   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
14148   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
14149   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
14150
14151   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
14152   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
14153
14154   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
14155   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
14156   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
14157   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
14158   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
14159   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
14160
14161   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
14162   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
14163   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
14164   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
14165
14166   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
14167   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
14168   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
14169   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
14170   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
14171   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
14172   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
14173   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
14174
14175   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
14176   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
14177   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
14178
14179   { MASK_SSE2, CODE_FOR_sse2_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
14180   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
14181
14182   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
14183   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
14184
14185   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
14186   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
14187   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
14188
14189   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
14190   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
14191   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
14192
14193   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
14194   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
14195
14196   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
14197
14198   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
14199   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
14200   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
14201   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
14202
14203   /* SSE3 MMX */
14204   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
14205   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
14206   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
14207   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
14208   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
14209   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 }
14210 };
14211
14212 static const struct builtin_description bdesc_1arg[] =
14213 {
14214   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
14215   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
14216
14217   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
14218   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
14219   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
14220
14221   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
14222   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
14223   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
14224   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
14225   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
14226   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
14227
14228   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
14229   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
14230
14231   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
14232
14233   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
14234   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
14235
14236   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
14237   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
14238   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
14239   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
14240   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
14241
14242   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
14243
14244   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
14245   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
14246   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
14247   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
14248
14249   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
14250   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
14251   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
14252
14253   /* SSE3 */
14254   { MASK_SSE3, CODE_FOR_sse3_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
14255   { MASK_SSE3, CODE_FOR_sse3_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
14256 };
14257
14258 static void
14259 ix86_init_builtins (void)
14260 {
14261   if (TARGET_MMX)
14262     ix86_init_mmx_sse_builtins ();
14263 }
14264
14265 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
14266    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
14267    builtins.  */
14268 static void
14269 ix86_init_mmx_sse_builtins (void)
14270 {
14271   const struct builtin_description * d;
14272   size_t i;
14273
14274   tree V16QI_type_node = build_vector_type_for_mode (intQI_type_node, V16QImode);
14275   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
14276   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
14277   tree V2DI_type_node
14278     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
14279   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
14280   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
14281   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
14282   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
14283   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
14284   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
14285
14286   tree pchar_type_node = build_pointer_type (char_type_node);
14287   tree pcchar_type_node = build_pointer_type (
14288                              build_type_variant (char_type_node, 1, 0));
14289   tree pfloat_type_node = build_pointer_type (float_type_node);
14290   tree pcfloat_type_node = build_pointer_type (
14291                              build_type_variant (float_type_node, 1, 0));
14292   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
14293   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
14294   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
14295
14296   /* Comparisons.  */
14297   tree int_ftype_v4sf_v4sf
14298     = build_function_type_list (integer_type_node,
14299                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14300   tree v4si_ftype_v4sf_v4sf
14301     = build_function_type_list (V4SI_type_node,
14302                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14303   /* MMX/SSE/integer conversions.  */
14304   tree int_ftype_v4sf
14305     = build_function_type_list (integer_type_node,
14306                                 V4SF_type_node, NULL_TREE);
14307   tree int64_ftype_v4sf
14308     = build_function_type_list (long_long_integer_type_node,
14309                                 V4SF_type_node, NULL_TREE);
14310   tree int_ftype_v8qi
14311     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
14312   tree v4sf_ftype_v4sf_int
14313     = build_function_type_list (V4SF_type_node,
14314                                 V4SF_type_node, integer_type_node, NULL_TREE);
14315   tree v4sf_ftype_v4sf_int64
14316     = build_function_type_list (V4SF_type_node,
14317                                 V4SF_type_node, long_long_integer_type_node,
14318                                 NULL_TREE);
14319   tree v4sf_ftype_v4sf_v2si
14320     = build_function_type_list (V4SF_type_node,
14321                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
14322
14323   /* Miscellaneous.  */
14324   tree v8qi_ftype_v4hi_v4hi
14325     = build_function_type_list (V8QI_type_node,
14326                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14327   tree v4hi_ftype_v2si_v2si
14328     = build_function_type_list (V4HI_type_node,
14329                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
14330   tree v4sf_ftype_v4sf_v4sf_int
14331     = build_function_type_list (V4SF_type_node,
14332                                 V4SF_type_node, V4SF_type_node,
14333                                 integer_type_node, NULL_TREE);
14334   tree v2si_ftype_v4hi_v4hi
14335     = build_function_type_list (V2SI_type_node,
14336                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14337   tree v4hi_ftype_v4hi_int
14338     = build_function_type_list (V4HI_type_node,
14339                                 V4HI_type_node, integer_type_node, NULL_TREE);
14340   tree v4hi_ftype_v4hi_di
14341     = build_function_type_list (V4HI_type_node,
14342                                 V4HI_type_node, long_long_unsigned_type_node,
14343                                 NULL_TREE);
14344   tree v2si_ftype_v2si_di
14345     = build_function_type_list (V2SI_type_node,
14346                                 V2SI_type_node, long_long_unsigned_type_node,
14347                                 NULL_TREE);
14348   tree void_ftype_void
14349     = build_function_type (void_type_node, void_list_node);
14350   tree void_ftype_unsigned
14351     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
14352   tree void_ftype_unsigned_unsigned
14353     = build_function_type_list (void_type_node, unsigned_type_node,
14354                                 unsigned_type_node, NULL_TREE);
14355   tree void_ftype_pcvoid_unsigned_unsigned
14356     = build_function_type_list (void_type_node, const_ptr_type_node,
14357                                 unsigned_type_node, unsigned_type_node,
14358                                 NULL_TREE);
14359   tree unsigned_ftype_void
14360     = build_function_type (unsigned_type_node, void_list_node);
14361   tree v2si_ftype_v4sf
14362     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
14363   /* Loads/stores.  */
14364   tree void_ftype_v8qi_v8qi_pchar
14365     = build_function_type_list (void_type_node,
14366                                 V8QI_type_node, V8QI_type_node,
14367                                 pchar_type_node, NULL_TREE);
14368   tree v4sf_ftype_pcfloat
14369     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
14370   /* @@@ the type is bogus */
14371   tree v4sf_ftype_v4sf_pv2si
14372     = build_function_type_list (V4SF_type_node,
14373                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
14374   tree void_ftype_pv2si_v4sf
14375     = build_function_type_list (void_type_node,
14376                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
14377   tree void_ftype_pfloat_v4sf
14378     = build_function_type_list (void_type_node,
14379                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
14380   tree void_ftype_pdi_di
14381     = build_function_type_list (void_type_node,
14382                                 pdi_type_node, long_long_unsigned_type_node,
14383                                 NULL_TREE);
14384   tree void_ftype_pv2di_v2di
14385     = build_function_type_list (void_type_node,
14386                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
14387   /* Normal vector unops.  */
14388   tree v4sf_ftype_v4sf
14389     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
14390
14391   /* Normal vector binops.  */
14392   tree v4sf_ftype_v4sf_v4sf
14393     = build_function_type_list (V4SF_type_node,
14394                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14395   tree v8qi_ftype_v8qi_v8qi
14396     = build_function_type_list (V8QI_type_node,
14397                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
14398   tree v4hi_ftype_v4hi_v4hi
14399     = build_function_type_list (V4HI_type_node,
14400                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14401   tree v2si_ftype_v2si_v2si
14402     = build_function_type_list (V2SI_type_node,
14403                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
14404   tree di_ftype_di_di
14405     = build_function_type_list (long_long_unsigned_type_node,
14406                                 long_long_unsigned_type_node,
14407                                 long_long_unsigned_type_node, NULL_TREE);
14408
14409   tree v2si_ftype_v2sf
14410     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
14411   tree v2sf_ftype_v2si
14412     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
14413   tree v2si_ftype_v2si
14414     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
14415   tree v2sf_ftype_v2sf
14416     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
14417   tree v2sf_ftype_v2sf_v2sf
14418     = build_function_type_list (V2SF_type_node,
14419                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
14420   tree v2si_ftype_v2sf_v2sf
14421     = build_function_type_list (V2SI_type_node,
14422                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
14423   tree pint_type_node    = build_pointer_type (integer_type_node);
14424   tree pdouble_type_node = build_pointer_type (double_type_node);
14425   tree pcdouble_type_node = build_pointer_type (
14426                                 build_type_variant (double_type_node, 1, 0));
14427   tree int_ftype_v2df_v2df
14428     = build_function_type_list (integer_type_node,
14429                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
14430
14431   tree void_ftype_pcvoid
14432     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
14433   tree v4sf_ftype_v4si
14434     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
14435   tree v4si_ftype_v4sf
14436     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
14437   tree v2df_ftype_v4si
14438     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
14439   tree v4si_ftype_v2df
14440     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
14441   tree v2si_ftype_v2df
14442     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
14443   tree v4sf_ftype_v2df
14444     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
14445   tree v2df_ftype_v2si
14446     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
14447   tree v2df_ftype_v4sf
14448     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
14449   tree int_ftype_v2df
14450     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
14451   tree int64_ftype_v2df
14452     = build_function_type_list (long_long_integer_type_node,
14453                                 V2DF_type_node, NULL_TREE);
14454   tree v2df_ftype_v2df_int
14455     = build_function_type_list (V2DF_type_node,
14456                                 V2DF_type_node, integer_type_node, NULL_TREE);
14457   tree v2df_ftype_v2df_int64
14458     = build_function_type_list (V2DF_type_node,
14459                                 V2DF_type_node, long_long_integer_type_node,
14460                                 NULL_TREE);
14461   tree v4sf_ftype_v4sf_v2df
14462     = build_function_type_list (V4SF_type_node,
14463                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
14464   tree v2df_ftype_v2df_v4sf
14465     = build_function_type_list (V2DF_type_node,
14466                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
14467   tree v2df_ftype_v2df_v2df_int
14468     = build_function_type_list (V2DF_type_node,
14469                                 V2DF_type_node, V2DF_type_node,
14470                                 integer_type_node,
14471                                 NULL_TREE);
14472   tree v2df_ftype_v2df_pcdouble
14473     = build_function_type_list (V2DF_type_node,
14474                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
14475   tree void_ftype_pdouble_v2df
14476     = build_function_type_list (void_type_node,
14477                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
14478   tree void_ftype_pint_int
14479     = build_function_type_list (void_type_node,
14480                                 pint_type_node, integer_type_node, NULL_TREE);
14481   tree void_ftype_v16qi_v16qi_pchar
14482     = build_function_type_list (void_type_node,
14483                                 V16QI_type_node, V16QI_type_node,
14484                                 pchar_type_node, NULL_TREE);
14485   tree v2df_ftype_pcdouble
14486     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
14487   tree v2df_ftype_v2df_v2df
14488     = build_function_type_list (V2DF_type_node,
14489                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
14490   tree v16qi_ftype_v16qi_v16qi
14491     = build_function_type_list (V16QI_type_node,
14492                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
14493   tree v8hi_ftype_v8hi_v8hi
14494     = build_function_type_list (V8HI_type_node,
14495                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
14496   tree v4si_ftype_v4si_v4si
14497     = build_function_type_list (V4SI_type_node,
14498                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
14499   tree v2di_ftype_v2di_v2di
14500     = build_function_type_list (V2DI_type_node,
14501                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
14502   tree v2di_ftype_v2df_v2df
14503     = build_function_type_list (V2DI_type_node,
14504                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
14505   tree v2df_ftype_v2df
14506     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
14507   tree v2di_ftype_v2di_int
14508     = build_function_type_list (V2DI_type_node,
14509                                 V2DI_type_node, integer_type_node, NULL_TREE);
14510   tree v4si_ftype_v4si_int
14511     = build_function_type_list (V4SI_type_node,
14512                                 V4SI_type_node, integer_type_node, NULL_TREE);
14513   tree v8hi_ftype_v8hi_int
14514     = build_function_type_list (V8HI_type_node,
14515                                 V8HI_type_node, integer_type_node, NULL_TREE);
14516   tree v8hi_ftype_v8hi_v2di
14517     = build_function_type_list (V8HI_type_node,
14518                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
14519   tree v4si_ftype_v4si_v2di
14520     = build_function_type_list (V4SI_type_node,
14521                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
14522   tree v4si_ftype_v8hi_v8hi
14523     = build_function_type_list (V4SI_type_node,
14524                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
14525   tree di_ftype_v8qi_v8qi
14526     = build_function_type_list (long_long_unsigned_type_node,
14527                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
14528   tree di_ftype_v2si_v2si
14529     = build_function_type_list (long_long_unsigned_type_node,
14530                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
14531   tree v2di_ftype_v16qi_v16qi
14532     = build_function_type_list (V2DI_type_node,
14533                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
14534   tree v2di_ftype_v4si_v4si
14535     = build_function_type_list (V2DI_type_node,
14536                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
14537   tree int_ftype_v16qi
14538     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
14539   tree v16qi_ftype_pcchar
14540     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
14541   tree void_ftype_pchar_v16qi
14542     = build_function_type_list (void_type_node,
14543                                 pchar_type_node, V16QI_type_node, NULL_TREE);
14544
14545   tree float80_type;
14546   tree float128_type;
14547   tree ftype;
14548
14549   /* The __float80 type.  */
14550   if (TYPE_MODE (long_double_type_node) == XFmode)
14551     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
14552                                                "__float80");
14553   else
14554     {
14555       /* The __float80 type.  */
14556       float80_type = make_node (REAL_TYPE);
14557       TYPE_PRECISION (float80_type) = 80;
14558       layout_type (float80_type);
14559       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
14560     }
14561
14562   float128_type = make_node (REAL_TYPE);
14563   TYPE_PRECISION (float128_type) = 128;
14564   layout_type (float128_type);
14565   (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
14566
14567   /* Add all builtins that are more or less simple operations on two
14568      operands.  */
14569   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14570     {
14571       /* Use one of the operands; the target can have a different mode for
14572          mask-generating compares.  */
14573       enum machine_mode mode;
14574       tree type;
14575
14576       if (d->name == 0)
14577         continue;
14578       mode = insn_data[d->icode].operand[1].mode;
14579
14580       switch (mode)
14581         {
14582         case V16QImode:
14583           type = v16qi_ftype_v16qi_v16qi;
14584           break;
14585         case V8HImode:
14586           type = v8hi_ftype_v8hi_v8hi;
14587           break;
14588         case V4SImode:
14589           type = v4si_ftype_v4si_v4si;
14590           break;
14591         case V2DImode:
14592           type = v2di_ftype_v2di_v2di;
14593           break;
14594         case V2DFmode:
14595           type = v2df_ftype_v2df_v2df;
14596           break;
14597         case V4SFmode:
14598           type = v4sf_ftype_v4sf_v4sf;
14599           break;
14600         case V8QImode:
14601           type = v8qi_ftype_v8qi_v8qi;
14602           break;
14603         case V4HImode:
14604           type = v4hi_ftype_v4hi_v4hi;
14605           break;
14606         case V2SImode:
14607           type = v2si_ftype_v2si_v2si;
14608           break;
14609         case DImode:
14610           type = di_ftype_di_di;
14611           break;
14612
14613         default:
14614           gcc_unreachable ();
14615         }
14616
14617       /* Override for comparisons.  */
14618       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
14619           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
14620         type = v4si_ftype_v4sf_v4sf;
14621
14622       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
14623           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
14624         type = v2di_ftype_v2df_v2df;
14625
14626       def_builtin (d->mask, d->name, type, d->code);
14627     }
14628
14629   /* Add the remaining MMX insns with somewhat more complicated types.  */
14630   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
14631   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
14632   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
14633   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
14634
14635   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
14636   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
14637   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
14638
14639   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
14640   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
14641
14642   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
14643   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
14644
14645   /* comi/ucomi insns.  */
14646   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
14647     if (d->mask == MASK_SSE2)
14648       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
14649     else
14650       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
14651
14652   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
14653   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
14654   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
14655
14656   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
14657   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
14658   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
14659   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
14660   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
14661   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
14662   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
14663   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
14664   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
14665   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
14666   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
14667
14668   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
14669
14670   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
14671   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
14672
14673   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
14674   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
14675   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
14676   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
14677
14678   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
14679   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
14680   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
14681   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
14682
14683   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
14684
14685   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
14686
14687   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
14688   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
14689   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
14690   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
14691   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
14692   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
14693
14694   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
14695
14696   /* Original 3DNow!  */
14697   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
14698   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
14699   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
14700   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
14701   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
14702   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
14703   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
14704   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
14705   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
14706   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
14707   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
14708   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
14709   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
14710   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
14711   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
14712   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
14713   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
14714   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
14715   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
14716   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
14717
14718   /* 3DNow! extension as used in the Athlon CPU.  */
14719   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
14720   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
14721   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
14722   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
14723   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
14724   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
14725
14726   /* SSE2 */
14727   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
14728
14729   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
14730   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
14731
14732   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
14733   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
14734
14735   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
14736   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
14737   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
14738   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
14739   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
14740
14741   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
14742   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
14743   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
14744   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
14745
14746   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
14747   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
14748
14749   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
14750
14751   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
14752   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
14753
14754   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
14755   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
14756   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
14757   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
14758   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
14759
14760   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
14761
14762   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
14763   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
14764   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
14765   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
14766
14767   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
14768   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
14769   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
14770
14771   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
14772   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
14773   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
14774   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
14775
14776   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
14777   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
14778   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
14779
14780   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
14781   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
14782
14783   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
14784   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
14785
14786   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
14787   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
14788   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
14789
14790   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
14791   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
14792   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
14793
14794   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
14795   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
14796
14797   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
14798   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
14799   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
14800   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
14801
14802   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
14803   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
14804   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
14805   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
14806
14807   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
14808   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
14809
14810   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
14811
14812   /* Prescott New Instructions.  */
14813   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
14814                void_ftype_pcvoid_unsigned_unsigned,
14815                IX86_BUILTIN_MONITOR);
14816   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
14817                void_ftype_unsigned_unsigned,
14818                IX86_BUILTIN_MWAIT);
14819   def_builtin (MASK_SSE3, "__builtin_ia32_movshdup",
14820                v4sf_ftype_v4sf,
14821                IX86_BUILTIN_MOVSHDUP);
14822   def_builtin (MASK_SSE3, "__builtin_ia32_movsldup",
14823                v4sf_ftype_v4sf,
14824                IX86_BUILTIN_MOVSLDUP);
14825   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
14826                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
14827
14828   /* Access to the vec_init patterns.  */
14829   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
14830                                     integer_type_node, NULL_TREE);
14831   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
14832                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
14833
14834   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
14835                                     short_integer_type_node,
14836                                     short_integer_type_node,
14837                                     short_integer_type_node, NULL_TREE);
14838   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
14839                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
14840
14841   ftype = build_function_type_list (V8QI_type_node, char_type_node,
14842                                     char_type_node, char_type_node,
14843                                     char_type_node, char_type_node,
14844                                     char_type_node, char_type_node,
14845                                     char_type_node, NULL_TREE);
14846   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
14847                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
14848
14849   /* Access to the vec_extract patterns.  */
14850   ftype = build_function_type_list (double_type_node, V2DF_type_node,
14851                                     integer_type_node, NULL_TREE);
14852   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
14853                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
14854
14855   ftype = build_function_type_list (long_long_integer_type_node,
14856                                     V2DI_type_node, integer_type_node,
14857                                     NULL_TREE);
14858   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
14859                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
14860
14861   ftype = build_function_type_list (float_type_node, V4SF_type_node,
14862                                     integer_type_node, NULL_TREE);
14863   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
14864                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
14865
14866   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
14867                                     integer_type_node, NULL_TREE);
14868   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
14869                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
14870
14871   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
14872                                     integer_type_node, NULL_TREE);
14873   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
14874                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
14875
14876   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
14877                                     integer_type_node, NULL_TREE);
14878   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
14879                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
14880
14881   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
14882                                     integer_type_node, NULL_TREE);
14883   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
14884                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
14885
14886   /* Access to the vec_set patterns.  */
14887   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
14888                                     intHI_type_node,
14889                                     integer_type_node, NULL_TREE);
14890   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
14891                ftype, IX86_BUILTIN_VEC_SET_V8HI);
14892   
14893   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
14894                                     intHI_type_node,
14895                                     integer_type_node, NULL_TREE);
14896   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
14897                ftype, IX86_BUILTIN_VEC_SET_V4HI);
14898 }
14899
14900 /* Errors in the source file can cause expand_expr to return const0_rtx
14901    where we expect a vector.  To avoid crashing, use one of the vector
14902    clear instructions.  */
14903 static rtx
14904 safe_vector_operand (rtx x, enum machine_mode mode)
14905 {
14906   if (x == const0_rtx)
14907     x = CONST0_RTX (mode);
14908   return x;
14909 }
14910
14911 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
14912
14913 static rtx
14914 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
14915 {
14916   rtx pat, xops[3];
14917   tree arg0 = TREE_VALUE (arglist);
14918   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14919   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14920   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14921   enum machine_mode tmode = insn_data[icode].operand[0].mode;
14922   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
14923   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
14924
14925   if (VECTOR_MODE_P (mode0))
14926     op0 = safe_vector_operand (op0, mode0);
14927   if (VECTOR_MODE_P (mode1))
14928     op1 = safe_vector_operand (op1, mode1);
14929
14930   if (optimize || !target
14931       || GET_MODE (target) != tmode
14932       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14933     target = gen_reg_rtx (tmode);
14934
14935   if (GET_MODE (op1) == SImode && mode1 == TImode)
14936     {
14937       rtx x = gen_reg_rtx (V4SImode);
14938       emit_insn (gen_sse2_loadd (x, op1));
14939       op1 = gen_lowpart (TImode, x);
14940     }
14941
14942   /* The insn must want input operands in the same modes as the
14943      result.  */
14944   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
14945               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
14946
14947   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
14948     op0 = copy_to_mode_reg (mode0, op0);
14949   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
14950     op1 = copy_to_mode_reg (mode1, op1);
14951
14952   /* ??? Using ix86_fixup_binary_operands is problematic when
14953      we've got mismatched modes.  Fake it.  */
14954
14955   xops[0] = target;
14956   xops[1] = op0;
14957   xops[2] = op1;
14958
14959   if (tmode == mode0 && tmode == mode1)
14960     {
14961       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
14962       op0 = xops[1];
14963       op1 = xops[2];
14964     }
14965   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
14966     {
14967       op0 = force_reg (mode0, op0);
14968       op1 = force_reg (mode1, op1);
14969       target = gen_reg_rtx (tmode);
14970     }
14971
14972   pat = GEN_FCN (icode) (target, op0, op1);
14973   if (! pat)
14974     return 0;
14975   emit_insn (pat);
14976   return target;
14977 }
14978
14979 /* Subroutine of ix86_expand_builtin to take care of stores.  */
14980
14981 static rtx
14982 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
14983 {
14984   rtx pat;
14985   tree arg0 = TREE_VALUE (arglist);
14986   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14987   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14988   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14989   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
14990   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
14991
14992   if (VECTOR_MODE_P (mode1))
14993     op1 = safe_vector_operand (op1, mode1);
14994
14995   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
14996   op1 = copy_to_mode_reg (mode1, op1);
14997
14998   pat = GEN_FCN (icode) (op0, op1);
14999   if (pat)
15000     emit_insn (pat);
15001   return 0;
15002 }
15003
15004 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
15005
15006 static rtx
15007 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
15008                           rtx target, int do_load)
15009 {
15010   rtx pat;
15011   tree arg0 = TREE_VALUE (arglist);
15012   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15013   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15014   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15015
15016   if (optimize || !target
15017       || GET_MODE (target) != tmode
15018       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15019     target = gen_reg_rtx (tmode);
15020   if (do_load)
15021     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15022   else
15023     {
15024       if (VECTOR_MODE_P (mode0))
15025         op0 = safe_vector_operand (op0, mode0);
15026
15027       if ((optimize && !register_operand (op0, mode0))
15028           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15029         op0 = copy_to_mode_reg (mode0, op0);
15030     }
15031
15032   pat = GEN_FCN (icode) (target, op0);
15033   if (! pat)
15034     return 0;
15035   emit_insn (pat);
15036   return target;
15037 }
15038
15039 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
15040    sqrtss, rsqrtss, rcpss.  */
15041
15042 static rtx
15043 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
15044 {
15045   rtx pat;
15046   tree arg0 = TREE_VALUE (arglist);
15047   rtx op1, op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15048   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15049   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15050
15051   if (optimize || !target
15052       || GET_MODE (target) != tmode
15053       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15054     target = gen_reg_rtx (tmode);
15055
15056   if (VECTOR_MODE_P (mode0))
15057     op0 = safe_vector_operand (op0, mode0);
15058
15059   if ((optimize && !register_operand (op0, mode0))
15060       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15061     op0 = copy_to_mode_reg (mode0, op0);
15062
15063   op1 = op0;
15064   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
15065     op1 = copy_to_mode_reg (mode0, op1);
15066
15067   pat = GEN_FCN (icode) (target, op0, op1);
15068   if (! pat)
15069     return 0;
15070   emit_insn (pat);
15071   return target;
15072 }
15073
15074 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
15075
15076 static rtx
15077 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
15078                          rtx target)
15079 {
15080   rtx pat;
15081   tree arg0 = TREE_VALUE (arglist);
15082   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15083   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15084   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15085   rtx op2;
15086   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
15087   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
15088   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
15089   enum rtx_code comparison = d->comparison;
15090
15091   if (VECTOR_MODE_P (mode0))
15092     op0 = safe_vector_operand (op0, mode0);
15093   if (VECTOR_MODE_P (mode1))
15094     op1 = safe_vector_operand (op1, mode1);
15095
15096   /* Swap operands if we have a comparison that isn't available in
15097      hardware.  */
15098   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15099     {
15100       rtx tmp = gen_reg_rtx (mode1);
15101       emit_move_insn (tmp, op1);
15102       op1 = op0;
15103       op0 = tmp;
15104     }
15105
15106   if (optimize || !target
15107       || GET_MODE (target) != tmode
15108       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
15109     target = gen_reg_rtx (tmode);
15110
15111   if ((optimize && !register_operand (op0, mode0))
15112       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
15113     op0 = copy_to_mode_reg (mode0, op0);
15114   if ((optimize && !register_operand (op1, mode1))
15115       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
15116     op1 = copy_to_mode_reg (mode1, op1);
15117
15118   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15119   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
15120   if (! pat)
15121     return 0;
15122   emit_insn (pat);
15123   return target;
15124 }
15125
15126 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
15127
15128 static rtx
15129 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
15130                       rtx target)
15131 {
15132   rtx pat;
15133   tree arg0 = TREE_VALUE (arglist);
15134   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15135   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15136   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15137   rtx op2;
15138   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
15139   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
15140   enum rtx_code comparison = d->comparison;
15141
15142   if (VECTOR_MODE_P (mode0))
15143     op0 = safe_vector_operand (op0, mode0);
15144   if (VECTOR_MODE_P (mode1))
15145     op1 = safe_vector_operand (op1, mode1);
15146
15147   /* Swap operands if we have a comparison that isn't available in
15148      hardware.  */
15149   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15150     {
15151       rtx tmp = op1;
15152       op1 = op0;
15153       op0 = tmp;
15154     }
15155
15156   target = gen_reg_rtx (SImode);
15157   emit_move_insn (target, const0_rtx);
15158   target = gen_rtx_SUBREG (QImode, target, 0);
15159
15160   if ((optimize && !register_operand (op0, mode0))
15161       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
15162     op0 = copy_to_mode_reg (mode0, op0);
15163   if ((optimize && !register_operand (op1, mode1))
15164       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
15165     op1 = copy_to_mode_reg (mode1, op1);
15166
15167   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15168   pat = GEN_FCN (d->icode) (op0, op1);
15169   if (! pat)
15170     return 0;
15171   emit_insn (pat);
15172   emit_insn (gen_rtx_SET (VOIDmode,
15173                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
15174                           gen_rtx_fmt_ee (comparison, QImode,
15175                                           SET_DEST (pat),
15176                                           const0_rtx)));
15177
15178   return SUBREG_REG (target);
15179 }
15180
15181 /* Return the integer constant in ARG.  Constrain it to be in the range
15182    of the subparts of VEC_TYPE; issue an error if not.  */
15183
15184 static int
15185 get_element_number (tree vec_type, tree arg)
15186 {
15187   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
15188
15189   if (!host_integerp (arg, 1)
15190       || (elt = tree_low_cst (arg, 1), elt > max))
15191     {
15192       error ("selector must be an integer constant in the range 0..%wi", max);
15193       return 0;
15194     }
15195
15196   return elt;
15197 }
15198
15199 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15200    ix86_expand_vector_init.  We DO have language-level syntax for this, in
15201    the form of  (type){ init-list }.  Except that since we can't place emms
15202    instructions from inside the compiler, we can't allow the use of MMX
15203    registers unless the user explicitly asks for it.  So we do *not* define
15204    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
15205    we have builtins invoked by mmintrin.h that gives us license to emit 
15206    these sorts of instructions.  */
15207
15208 static rtx
15209 ix86_expand_vec_init_builtin (tree type, tree arglist, rtx target)
15210 {
15211   enum machine_mode tmode = TYPE_MODE (type);
15212   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
15213   int i, n_elt = GET_MODE_NUNITS (tmode);
15214   rtvec v = rtvec_alloc (n_elt);
15215
15216   gcc_assert (VECTOR_MODE_P (tmode));
15217
15218   for (i = 0; i < n_elt; ++i, arglist = TREE_CHAIN (arglist))
15219     {
15220       rtx x = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
15221       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
15222     }
15223
15224   gcc_assert (arglist == NULL);
15225
15226   if (!target || !register_operand (target, tmode))
15227     target = gen_reg_rtx (tmode);
15228
15229   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
15230   return target;
15231 }
15232
15233 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15234    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
15235    had a language-level syntax for referencing vector elements.  */
15236
15237 static rtx
15238 ix86_expand_vec_ext_builtin (tree arglist, rtx target)
15239 {
15240   enum machine_mode tmode, mode0;
15241   tree arg0, arg1;
15242   int elt;
15243   rtx op0;
15244
15245   arg0 = TREE_VALUE (arglist);
15246   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15247
15248   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15249   elt = get_element_number (TREE_TYPE (arg0), arg1);
15250
15251   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15252   mode0 = TYPE_MODE (TREE_TYPE (arg0));
15253   gcc_assert (VECTOR_MODE_P (mode0));
15254
15255   op0 = force_reg (mode0, op0);
15256
15257   if (optimize || !target || !register_operand (target, tmode))
15258     target = gen_reg_rtx (tmode);
15259
15260   ix86_expand_vector_extract (true, target, op0, elt);
15261
15262   return target;
15263 }
15264
15265 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15266    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
15267    a language-level syntax for referencing vector elements.  */
15268
15269 static rtx
15270 ix86_expand_vec_set_builtin (tree arglist)
15271 {
15272   enum machine_mode tmode, mode1;
15273   tree arg0, arg1, arg2;
15274   int elt;
15275   rtx op0, op1;
15276
15277   arg0 = TREE_VALUE (arglist);
15278   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15279   arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15280
15281   tmode = TYPE_MODE (TREE_TYPE (arg0));
15282   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15283   gcc_assert (VECTOR_MODE_P (tmode));
15284
15285   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
15286   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
15287   elt = get_element_number (TREE_TYPE (arg0), arg2);
15288
15289   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
15290     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
15291
15292   op0 = force_reg (tmode, op0);
15293   op1 = force_reg (mode1, op1);
15294
15295   ix86_expand_vector_set (true, op0, op1, elt);
15296
15297   return op0;
15298 }
15299
15300 /* Expand an expression EXP that calls a built-in function,
15301    with result going to TARGET if that's convenient
15302    (and in mode MODE if that's convenient).
15303    SUBTARGET may be used as the target for computing one of EXP's operands.
15304    IGNORE is nonzero if the value is to be ignored.  */
15305
15306 static rtx
15307 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
15308                      enum machine_mode mode ATTRIBUTE_UNUSED,
15309                      int ignore ATTRIBUTE_UNUSED)
15310 {
15311   const struct builtin_description *d;
15312   size_t i;
15313   enum insn_code icode;
15314   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
15315   tree arglist = TREE_OPERAND (exp, 1);
15316   tree arg0, arg1, arg2;
15317   rtx op0, op1, op2, pat;
15318   enum machine_mode tmode, mode0, mode1, mode2;
15319   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
15320
15321   switch (fcode)
15322     {
15323     case IX86_BUILTIN_EMMS:
15324       emit_insn (gen_mmx_emms ());
15325       return 0;
15326
15327     case IX86_BUILTIN_SFENCE:
15328       emit_insn (gen_sse_sfence ());
15329       return 0;
15330
15331     case IX86_BUILTIN_MASKMOVQ:
15332     case IX86_BUILTIN_MASKMOVDQU:
15333       icode = (fcode == IX86_BUILTIN_MASKMOVQ
15334                ? CODE_FOR_mmx_maskmovq
15335                : CODE_FOR_sse2_maskmovdqu);
15336       /* Note the arg order is different from the operand order.  */
15337       arg1 = TREE_VALUE (arglist);
15338       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
15339       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15340       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15341       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15342       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
15343       mode0 = insn_data[icode].operand[0].mode;
15344       mode1 = insn_data[icode].operand[1].mode;
15345       mode2 = insn_data[icode].operand[2].mode;
15346
15347       op0 = force_reg (Pmode, op0);
15348       op0 = gen_rtx_MEM (mode1, op0);
15349
15350       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
15351         op0 = copy_to_mode_reg (mode0, op0);
15352       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
15353         op1 = copy_to_mode_reg (mode1, op1);
15354       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
15355         op2 = copy_to_mode_reg (mode2, op2);
15356       pat = GEN_FCN (icode) (op0, op1, op2);
15357       if (! pat)
15358         return 0;
15359       emit_insn (pat);
15360       return 0;
15361
15362     case IX86_BUILTIN_SQRTSS:
15363       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, arglist, target);
15364     case IX86_BUILTIN_RSQRTSS:
15365       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, arglist, target);
15366     case IX86_BUILTIN_RCPSS:
15367       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, arglist, target);
15368
15369     case IX86_BUILTIN_LOADUPS:
15370       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
15371
15372     case IX86_BUILTIN_STOREUPS:
15373       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
15374
15375     case IX86_BUILTIN_LOADHPS:
15376     case IX86_BUILTIN_LOADLPS:
15377     case IX86_BUILTIN_LOADHPD:
15378     case IX86_BUILTIN_LOADLPD:
15379       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
15380                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
15381                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
15382                : CODE_FOR_sse2_loadlpd);
15383       arg0 = TREE_VALUE (arglist);
15384       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15385       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15386       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15387       tmode = insn_data[icode].operand[0].mode;
15388       mode0 = insn_data[icode].operand[1].mode;
15389       mode1 = insn_data[icode].operand[2].mode;
15390
15391       op0 = force_reg (mode0, op0);
15392       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
15393       if (optimize || target == 0
15394           || GET_MODE (target) != tmode
15395           || !register_operand (target, tmode))
15396         target = gen_reg_rtx (tmode);
15397       pat = GEN_FCN (icode) (target, op0, op1);
15398       if (! pat)
15399         return 0;
15400       emit_insn (pat);
15401       return target;
15402
15403     case IX86_BUILTIN_STOREHPS:
15404     case IX86_BUILTIN_STORELPS:
15405       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
15406                : CODE_FOR_sse_storelps);
15407       arg0 = TREE_VALUE (arglist);
15408       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15409       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15410       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15411       mode0 = insn_data[icode].operand[0].mode;
15412       mode1 = insn_data[icode].operand[1].mode;
15413
15414       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15415       op1 = force_reg (mode1, op1);
15416
15417       pat = GEN_FCN (icode) (op0, op1);
15418       if (! pat)
15419         return 0;
15420       emit_insn (pat);
15421       return const0_rtx;
15422
15423     case IX86_BUILTIN_MOVNTPS:
15424       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
15425     case IX86_BUILTIN_MOVNTQ:
15426       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
15427
15428     case IX86_BUILTIN_LDMXCSR:
15429       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
15430       target = assign_386_stack_local (SImode, SLOT_TEMP);
15431       emit_move_insn (target, op0);
15432       emit_insn (gen_sse_ldmxcsr (target));
15433       return 0;
15434
15435     case IX86_BUILTIN_STMXCSR:
15436       target = assign_386_stack_local (SImode, SLOT_TEMP);
15437       emit_insn (gen_sse_stmxcsr (target));
15438       return copy_to_mode_reg (SImode, target);
15439
15440     case IX86_BUILTIN_SHUFPS:
15441     case IX86_BUILTIN_SHUFPD:
15442       icode = (fcode == IX86_BUILTIN_SHUFPS
15443                ? CODE_FOR_sse_shufps
15444                : CODE_FOR_sse2_shufpd);
15445       arg0 = TREE_VALUE (arglist);
15446       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15447       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15448       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15449       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15450       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
15451       tmode = insn_data[icode].operand[0].mode;
15452       mode0 = insn_data[icode].operand[1].mode;
15453       mode1 = insn_data[icode].operand[2].mode;
15454       mode2 = insn_data[icode].operand[3].mode;
15455
15456       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15457         op0 = copy_to_mode_reg (mode0, op0);
15458       if ((optimize && !register_operand (op1, mode1))
15459           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
15460         op1 = copy_to_mode_reg (mode1, op1);
15461       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15462         {
15463           /* @@@ better error message */
15464           error ("mask must be an immediate");
15465           return gen_reg_rtx (tmode);
15466         }
15467       if (optimize || target == 0
15468           || GET_MODE (target) != tmode
15469           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15470         target = gen_reg_rtx (tmode);
15471       pat = GEN_FCN (icode) (target, op0, op1, op2);
15472       if (! pat)
15473         return 0;
15474       emit_insn (pat);
15475       return target;
15476
15477     case IX86_BUILTIN_PSHUFW:
15478     case IX86_BUILTIN_PSHUFD:
15479     case IX86_BUILTIN_PSHUFHW:
15480     case IX86_BUILTIN_PSHUFLW:
15481       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
15482                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
15483                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
15484                : CODE_FOR_mmx_pshufw);
15485       arg0 = TREE_VALUE (arglist);
15486       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15487       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15488       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15489       tmode = insn_data[icode].operand[0].mode;
15490       mode1 = insn_data[icode].operand[1].mode;
15491       mode2 = insn_data[icode].operand[2].mode;
15492
15493       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
15494         op0 = copy_to_mode_reg (mode1, op0);
15495       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
15496         {
15497           /* @@@ better error message */
15498           error ("mask must be an immediate");
15499           return const0_rtx;
15500         }
15501       if (target == 0
15502           || GET_MODE (target) != tmode
15503           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15504         target = gen_reg_rtx (tmode);
15505       pat = GEN_FCN (icode) (target, op0, op1);
15506       if (! pat)
15507         return 0;
15508       emit_insn (pat);
15509       return target;
15510
15511     case IX86_BUILTIN_PSLLDQI128:
15512     case IX86_BUILTIN_PSRLDQI128:
15513       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
15514                : CODE_FOR_sse2_lshrti3);
15515       arg0 = TREE_VALUE (arglist);
15516       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15517       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15518       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15519       tmode = insn_data[icode].operand[0].mode;
15520       mode1 = insn_data[icode].operand[1].mode;
15521       mode2 = insn_data[icode].operand[2].mode;
15522
15523       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
15524         {
15525           op0 = copy_to_reg (op0);
15526           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
15527         }
15528       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
15529         {
15530           error ("shift must be an immediate");
15531           return const0_rtx;
15532         }
15533       target = gen_reg_rtx (V2DImode);
15534       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
15535       if (! pat)
15536         return 0;
15537       emit_insn (pat);
15538       return target;
15539
15540     case IX86_BUILTIN_FEMMS:
15541       emit_insn (gen_mmx_femms ());
15542       return NULL_RTX;
15543
15544     case IX86_BUILTIN_PAVGUSB:
15545       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, arglist, target);
15546
15547     case IX86_BUILTIN_PF2ID:
15548       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, arglist, target, 0);
15549
15550     case IX86_BUILTIN_PFACC:
15551       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, arglist, target);
15552
15553     case IX86_BUILTIN_PFADD:
15554      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, arglist, target);
15555
15556     case IX86_BUILTIN_PFCMPEQ:
15557       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, arglist, target);
15558
15559     case IX86_BUILTIN_PFCMPGE:
15560       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, arglist, target);
15561
15562     case IX86_BUILTIN_PFCMPGT:
15563       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, arglist, target);
15564
15565     case IX86_BUILTIN_PFMAX:
15566       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, arglist, target);
15567
15568     case IX86_BUILTIN_PFMIN:
15569       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, arglist, target);
15570
15571     case IX86_BUILTIN_PFMUL:
15572       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, arglist, target);
15573
15574     case IX86_BUILTIN_PFRCP:
15575       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, arglist, target, 0);
15576
15577     case IX86_BUILTIN_PFRCPIT1:
15578       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, arglist, target);
15579
15580     case IX86_BUILTIN_PFRCPIT2:
15581       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, arglist, target);
15582
15583     case IX86_BUILTIN_PFRSQIT1:
15584       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, arglist, target);
15585
15586     case IX86_BUILTIN_PFRSQRT:
15587       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, arglist, target, 0);
15588
15589     case IX86_BUILTIN_PFSUB:
15590       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, arglist, target);
15591
15592     case IX86_BUILTIN_PFSUBR:
15593       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, arglist, target);
15594
15595     case IX86_BUILTIN_PI2FD:
15596       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, arglist, target, 0);
15597
15598     case IX86_BUILTIN_PMULHRW:
15599       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, arglist, target);
15600
15601     case IX86_BUILTIN_PF2IW:
15602       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, arglist, target, 0);
15603
15604     case IX86_BUILTIN_PFNACC:
15605       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, arglist, target);
15606
15607     case IX86_BUILTIN_PFPNACC:
15608       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, arglist, target);
15609
15610     case IX86_BUILTIN_PI2FW:
15611       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, arglist, target, 0);
15612
15613     case IX86_BUILTIN_PSWAPDSI:
15614       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, arglist, target, 0);
15615
15616     case IX86_BUILTIN_PSWAPDSF:
15617       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, arglist, target, 0);
15618
15619     case IX86_BUILTIN_SQRTSD:
15620       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, arglist, target);
15621     case IX86_BUILTIN_LOADUPD:
15622       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
15623     case IX86_BUILTIN_STOREUPD:
15624       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
15625
15626     case IX86_BUILTIN_MFENCE:
15627         emit_insn (gen_sse2_mfence ());
15628         return 0;
15629     case IX86_BUILTIN_LFENCE:
15630         emit_insn (gen_sse2_lfence ());
15631         return 0;
15632
15633     case IX86_BUILTIN_CLFLUSH:
15634         arg0 = TREE_VALUE (arglist);
15635         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15636         icode = CODE_FOR_sse2_clflush;
15637         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
15638             op0 = copy_to_mode_reg (Pmode, op0);
15639
15640         emit_insn (gen_sse2_clflush (op0));
15641         return 0;
15642
15643     case IX86_BUILTIN_MOVNTPD:
15644       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
15645     case IX86_BUILTIN_MOVNTDQ:
15646       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
15647     case IX86_BUILTIN_MOVNTI:
15648       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
15649
15650     case IX86_BUILTIN_LOADDQU:
15651       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
15652     case IX86_BUILTIN_STOREDQU:
15653       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
15654
15655     case IX86_BUILTIN_MONITOR:
15656       arg0 = TREE_VALUE (arglist);
15657       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15658       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15659       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15660       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15661       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
15662       if (!REG_P (op0))
15663         op0 = copy_to_mode_reg (SImode, op0);
15664       if (!REG_P (op1))
15665         op1 = copy_to_mode_reg (SImode, op1);
15666       if (!REG_P (op2))
15667         op2 = copy_to_mode_reg (SImode, op2);
15668       emit_insn (gen_sse3_monitor (op0, op1, op2));
15669       return 0;
15670
15671     case IX86_BUILTIN_MWAIT:
15672       arg0 = TREE_VALUE (arglist);
15673       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15674       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15675       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15676       if (!REG_P (op0))
15677         op0 = copy_to_mode_reg (SImode, op0);
15678       if (!REG_P (op1))
15679         op1 = copy_to_mode_reg (SImode, op1);
15680       emit_insn (gen_sse3_mwait (op0, op1));
15681       return 0;
15682
15683     case IX86_BUILTIN_LDDQU:
15684       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, arglist,
15685                                        target, 1);
15686
15687     case IX86_BUILTIN_VEC_INIT_V2SI:
15688     case IX86_BUILTIN_VEC_INIT_V4HI:
15689     case IX86_BUILTIN_VEC_INIT_V8QI:
15690       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), arglist, target);
15691
15692     case IX86_BUILTIN_VEC_EXT_V2DF:
15693     case IX86_BUILTIN_VEC_EXT_V2DI:
15694     case IX86_BUILTIN_VEC_EXT_V4SF:
15695     case IX86_BUILTIN_VEC_EXT_V4SI:
15696     case IX86_BUILTIN_VEC_EXT_V8HI:
15697     case IX86_BUILTIN_VEC_EXT_V2SI:
15698     case IX86_BUILTIN_VEC_EXT_V4HI:
15699       return ix86_expand_vec_ext_builtin (arglist, target);
15700
15701     case IX86_BUILTIN_VEC_SET_V8HI:
15702     case IX86_BUILTIN_VEC_SET_V4HI:
15703       return ix86_expand_vec_set_builtin (arglist);
15704
15705     default:
15706       break;
15707     }
15708
15709   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15710     if (d->code == fcode)
15711       {
15712         /* Compares are treated specially.  */
15713         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
15714             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
15715             || d->icode == CODE_FOR_sse2_maskcmpv2df3
15716             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
15717           return ix86_expand_sse_compare (d, arglist, target);
15718
15719         return ix86_expand_binop_builtin (d->icode, arglist, target);
15720       }
15721
15722   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15723     if (d->code == fcode)
15724       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
15725
15726   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
15727     if (d->code == fcode)
15728       return ix86_expand_sse_comi (d, arglist, target);
15729
15730   gcc_unreachable ();
15731 }
15732
15733 /* Store OPERAND to the memory after reload is completed.  This means
15734    that we can't easily use assign_stack_local.  */
15735 rtx
15736 ix86_force_to_memory (enum machine_mode mode, rtx operand)
15737 {
15738   rtx result;
15739   
15740   gcc_assert (reload_completed);
15741   if (TARGET_RED_ZONE)
15742     {
15743       result = gen_rtx_MEM (mode,
15744                             gen_rtx_PLUS (Pmode,
15745                                           stack_pointer_rtx,
15746                                           GEN_INT (-RED_ZONE_SIZE)));
15747       emit_move_insn (result, operand);
15748     }
15749   else if (!TARGET_RED_ZONE && TARGET_64BIT)
15750     {
15751       switch (mode)
15752         {
15753         case HImode:
15754         case SImode:
15755           operand = gen_lowpart (DImode, operand);
15756           /* FALLTHRU */
15757         case DImode:
15758           emit_insn (
15759                       gen_rtx_SET (VOIDmode,
15760                                    gen_rtx_MEM (DImode,
15761                                                 gen_rtx_PRE_DEC (DImode,
15762                                                         stack_pointer_rtx)),
15763                                    operand));
15764           break;
15765         default:
15766           gcc_unreachable ();
15767         }
15768       result = gen_rtx_MEM (mode, stack_pointer_rtx);
15769     }
15770   else
15771     {
15772       switch (mode)
15773         {
15774         case DImode:
15775           {
15776             rtx operands[2];
15777             split_di (&operand, 1, operands, operands + 1);
15778             emit_insn (
15779                         gen_rtx_SET (VOIDmode,
15780                                      gen_rtx_MEM (SImode,
15781                                                   gen_rtx_PRE_DEC (Pmode,
15782                                                         stack_pointer_rtx)),
15783                                      operands[1]));
15784             emit_insn (
15785                         gen_rtx_SET (VOIDmode,
15786                                      gen_rtx_MEM (SImode,
15787                                                   gen_rtx_PRE_DEC (Pmode,
15788                                                         stack_pointer_rtx)),
15789                                      operands[0]));
15790           }
15791           break;
15792         case HImode:
15793           /* Store HImodes as SImodes.  */
15794           operand = gen_lowpart (SImode, operand);
15795           /* FALLTHRU */
15796         case SImode:
15797           emit_insn (
15798                       gen_rtx_SET (VOIDmode,
15799                                    gen_rtx_MEM (GET_MODE (operand),
15800                                                 gen_rtx_PRE_DEC (SImode,
15801                                                         stack_pointer_rtx)),
15802                                    operand));
15803           break;
15804         default:
15805           gcc_unreachable ();
15806         }
15807       result = gen_rtx_MEM (mode, stack_pointer_rtx);
15808     }
15809   return result;
15810 }
15811
15812 /* Free operand from the memory.  */
15813 void
15814 ix86_free_from_memory (enum machine_mode mode)
15815 {
15816   if (!TARGET_RED_ZONE)
15817     {
15818       int size;
15819
15820       if (mode == DImode || TARGET_64BIT)
15821         size = 8;
15822       else
15823         size = 4;
15824       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
15825          to pop or add instruction if registers are available.  */
15826       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
15827                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
15828                                             GEN_INT (size))));
15829     }
15830 }
15831
15832 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
15833    QImode must go into class Q_REGS.
15834    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
15835    movdf to do mem-to-mem moves through integer regs.  */
15836 enum reg_class
15837 ix86_preferred_reload_class (rtx x, enum reg_class class)
15838 {
15839   /* We're only allowed to return a subclass of CLASS.  Many of the 
15840      following checks fail for NO_REGS, so eliminate that early.  */
15841   if (class == NO_REGS)
15842     return NO_REGS;
15843
15844   /* All classes can load zeros.  */
15845   if (x == CONST0_RTX (GET_MODE (x)))
15846     return class;
15847
15848   /* Floating-point constants need more complex checks.  */
15849   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
15850     {
15851       /* General regs can load everything.  */
15852       if (reg_class_subset_p (class, GENERAL_REGS))
15853         return class;
15854
15855       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
15856          zero above.  We only want to wind up preferring 80387 registers if
15857          we plan on doing computation with them.  */
15858       if (TARGET_80387
15859           && (TARGET_MIX_SSE_I387 
15860               || !(TARGET_SSE_MATH && SSE_FLOAT_MODE_P (GET_MODE (x))))
15861           && standard_80387_constant_p (x))
15862         {
15863           /* Limit class to non-sse.  */
15864           if (class == FLOAT_SSE_REGS)
15865             return FLOAT_REGS;
15866           if (class == FP_TOP_SSE_REGS)
15867             return FP_TOP_REG;
15868           if (class == FP_SECOND_SSE_REGS)
15869             return FP_SECOND_REG;
15870           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
15871             return class;
15872         }
15873
15874       return NO_REGS;
15875     }
15876   if (MAYBE_MMX_CLASS_P (class) && CONSTANT_P (x))
15877     return NO_REGS;
15878   if (MAYBE_SSE_CLASS_P (class) && CONSTANT_P (x))
15879     return NO_REGS;
15880
15881   /* Generally when we see PLUS here, it's the function invariant
15882      (plus soft-fp const_int).  Which can only be computed into general
15883      regs.  */
15884   if (GET_CODE (x) == PLUS)
15885     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
15886
15887   /* QImode constants are easy to load, but non-constant QImode data
15888      must go into Q_REGS.  */
15889   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
15890     {
15891       if (reg_class_subset_p (class, Q_REGS))
15892         return class;
15893       if (reg_class_subset_p (Q_REGS, class))
15894         return Q_REGS;
15895       return NO_REGS;
15896     }
15897
15898   return class;
15899 }
15900
15901 /* If we are copying between general and FP registers, we need a memory
15902    location. The same is true for SSE and MMX registers.
15903
15904    The macro can't work reliably when one of the CLASSES is class containing
15905    registers from multiple units (SSE, MMX, integer).  We avoid this by never
15906    combining those units in single alternative in the machine description.
15907    Ensure that this constraint holds to avoid unexpected surprises.
15908
15909    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
15910    enforce these sanity checks.  */
15911
15912 int
15913 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
15914                               enum machine_mode mode, int strict)
15915 {
15916   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
15917       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
15918       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
15919       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
15920       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
15921       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
15922     {
15923       gcc_assert (!strict);
15924       return true;
15925     }
15926
15927   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
15928     return true;
15929
15930   /* ??? This is a lie.  We do have moves between mmx/general, and for
15931      mmx/sse2.  But by saying we need secondary memory we discourage the
15932      register allocator from using the mmx registers unless needed.  */
15933   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
15934     return true;
15935
15936   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
15937     {
15938       /* SSE1 doesn't have any direct moves from other classes.  */
15939       if (!TARGET_SSE2)
15940         return true;
15941
15942       /* If the target says that inter-unit moves are more expensive 
15943          than moving through memory, then don't generate them.  */
15944       if (!TARGET_INTER_UNIT_MOVES && !optimize_size)
15945         return true;
15946
15947       /* Between SSE and general, we have moves no larger than word size.  */
15948       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
15949         return true;
15950
15951       /* ??? For the cost of one register reformat penalty, we could use
15952          the same instructions to move SFmode and DFmode data, but the 
15953          relevant move patterns don't support those alternatives.  */
15954       if (mode == SFmode || mode == DFmode)
15955         return true;
15956     }
15957
15958   return false;
15959 }
15960
15961 /* Return true if the registers in CLASS cannot represent the change from
15962    modes FROM to TO.  */
15963
15964 bool
15965 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
15966                                enum reg_class class)
15967 {
15968   if (from == to)
15969     return false;
15970
15971   /* x87 registers can't do subreg at all, as all values are reformatted
15972      to extended precision.  */
15973   if (MAYBE_FLOAT_CLASS_P (class))
15974     return true;
15975
15976   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
15977     {
15978       /* Vector registers do not support QI or HImode loads.  If we don't
15979          disallow a change to these modes, reload will assume it's ok to
15980          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
15981          the vec_dupv4hi pattern.  */
15982       if (GET_MODE_SIZE (from) < 4)
15983         return true;
15984
15985       /* Vector registers do not support subreg with nonzero offsets, which
15986          are otherwise valid for integer registers.  Since we can't see 
15987          whether we have a nonzero offset from here, prohibit all
15988          nonparadoxical subregs changing size.  */
15989       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
15990         return true;
15991     }
15992
15993   return false;
15994 }
15995
15996 /* Return the cost of moving data from a register in class CLASS1 to
15997    one in class CLASS2.
15998
15999    It is not required that the cost always equal 2 when FROM is the same as TO;
16000    on some machines it is expensive to move between registers if they are not
16001    general registers.  */
16002
16003 int
16004 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
16005                          enum reg_class class2)
16006 {
16007   /* In case we require secondary memory, compute cost of the store followed
16008      by load.  In order to avoid bad register allocation choices, we need
16009      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
16010
16011   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
16012     {
16013       int cost = 1;
16014
16015       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
16016                    MEMORY_MOVE_COST (mode, class1, 1));
16017       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
16018                    MEMORY_MOVE_COST (mode, class2, 1));
16019
16020       /* In case of copying from general_purpose_register we may emit multiple
16021          stores followed by single load causing memory size mismatch stall.
16022          Count this as arbitrarily high cost of 20.  */
16023       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
16024         cost += 20;
16025
16026       /* In the case of FP/MMX moves, the registers actually overlap, and we
16027          have to switch modes in order to treat them differently.  */
16028       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
16029           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
16030         cost += 20;
16031
16032       return cost;
16033     }
16034
16035   /* Moves between SSE/MMX and integer unit are expensive.  */
16036   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
16037       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
16038     return ix86_cost->mmxsse_to_integer;
16039   if (MAYBE_FLOAT_CLASS_P (class1))
16040     return ix86_cost->fp_move;
16041   if (MAYBE_SSE_CLASS_P (class1))
16042     return ix86_cost->sse_move;
16043   if (MAYBE_MMX_CLASS_P (class1))
16044     return ix86_cost->mmx_move;
16045   return 2;
16046 }
16047
16048 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
16049
16050 bool
16051 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
16052 {
16053   /* Flags and only flags can only hold CCmode values.  */
16054   if (CC_REGNO_P (regno))
16055     return GET_MODE_CLASS (mode) == MODE_CC;
16056   if (GET_MODE_CLASS (mode) == MODE_CC
16057       || GET_MODE_CLASS (mode) == MODE_RANDOM
16058       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
16059     return 0;
16060   if (FP_REGNO_P (regno))
16061     return VALID_FP_MODE_P (mode);
16062   if (SSE_REGNO_P (regno))
16063     {
16064       /* We implement the move patterns for all vector modes into and
16065          out of SSE registers, even when no operation instructions
16066          are available.  */
16067       return (VALID_SSE_REG_MODE (mode)
16068               || VALID_SSE2_REG_MODE (mode)
16069               || VALID_MMX_REG_MODE (mode)
16070               || VALID_MMX_REG_MODE_3DNOW (mode));
16071     }
16072   if (MMX_REGNO_P (regno))
16073     {
16074       /* We implement the move patterns for 3DNOW modes even in MMX mode,
16075          so if the register is available at all, then we can move data of
16076          the given mode into or out of it.  */
16077       return (VALID_MMX_REG_MODE (mode)
16078               || VALID_MMX_REG_MODE_3DNOW (mode));
16079     }
16080
16081   if (mode == QImode)
16082     {
16083       /* Take care for QImode values - they can be in non-QI regs,
16084          but then they do cause partial register stalls.  */
16085       if (regno < 4 || TARGET_64BIT)
16086         return 1;
16087       if (!TARGET_PARTIAL_REG_STALL)
16088         return 1;
16089       return reload_in_progress || reload_completed;
16090     }
16091   /* We handle both integer and floats in the general purpose registers.  */
16092   else if (VALID_INT_MODE_P (mode))
16093     return 1;
16094   else if (VALID_FP_MODE_P (mode))
16095     return 1;
16096   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
16097      on to use that value in smaller contexts, this can easily force a 
16098      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
16099      supporting DImode, allow it.  */
16100   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
16101     return 1;
16102
16103   return 0;
16104 }
16105
16106 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a 
16107    tieable integer mode.  */
16108
16109 static bool
16110 ix86_tieable_integer_mode_p (enum machine_mode mode)
16111 {
16112   switch (mode)
16113     {
16114     case HImode:
16115     case SImode:
16116       return true;
16117
16118     case QImode:
16119       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
16120
16121     case DImode:
16122       return TARGET_64BIT;
16123
16124     default:
16125       return false;
16126     }
16127 }
16128
16129 /* Return true if MODE1 is accessible in a register that can hold MODE2
16130    without copying.  That is, all register classes that can hold MODE2
16131    can also hold MODE1.  */
16132
16133 bool
16134 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
16135 {
16136   if (mode1 == mode2)
16137     return true;
16138
16139   if (ix86_tieable_integer_mode_p (mode1)
16140       && ix86_tieable_integer_mode_p (mode2))
16141     return true;
16142
16143   /* MODE2 being XFmode implies fp stack or general regs, which means we
16144      can tie any smaller floating point modes to it.  Note that we do not
16145      tie this with TFmode.  */
16146   if (mode2 == XFmode)
16147     return mode1 == SFmode || mode1 == DFmode;
16148
16149   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
16150      that we can tie it with SFmode.  */
16151   if (mode2 == DFmode)
16152     return mode1 == SFmode;
16153
16154   /* If MODE2 is only appropriate for an SSE register, then tie with 
16155      any other mode acceptable to SSE registers.  */
16156   if (GET_MODE_SIZE (mode2) >= 8
16157       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
16158     return ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1);
16159
16160   /* If MODE2 is appropriate for an MMX (or SSE) register, then tie
16161      with any other mode acceptable to MMX registers.  */
16162   if (GET_MODE_SIZE (mode2) == 8
16163       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
16164     return ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1);
16165
16166   return false;
16167 }
16168
16169 /* Return the cost of moving data of mode M between a
16170    register and memory.  A value of 2 is the default; this cost is
16171    relative to those in `REGISTER_MOVE_COST'.
16172
16173    If moving between registers and memory is more expensive than
16174    between two registers, you should define this macro to express the
16175    relative cost.
16176
16177    Model also increased moving costs of QImode registers in non
16178    Q_REGS classes.
16179  */
16180 int
16181 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
16182 {
16183   if (FLOAT_CLASS_P (class))
16184     {
16185       int index;
16186       switch (mode)
16187         {
16188           case SFmode:
16189             index = 0;
16190             break;
16191           case DFmode:
16192             index = 1;
16193             break;
16194           case XFmode:
16195             index = 2;
16196             break;
16197           default:
16198             return 100;
16199         }
16200       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
16201     }
16202   if (SSE_CLASS_P (class))
16203     {
16204       int index;
16205       switch (GET_MODE_SIZE (mode))
16206         {
16207           case 4:
16208             index = 0;
16209             break;
16210           case 8:
16211             index = 1;
16212             break;
16213           case 16:
16214             index = 2;
16215             break;
16216           default:
16217             return 100;
16218         }
16219       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
16220     }
16221   if (MMX_CLASS_P (class))
16222     {
16223       int index;
16224       switch (GET_MODE_SIZE (mode))
16225         {
16226           case 4:
16227             index = 0;
16228             break;
16229           case 8:
16230             index = 1;
16231             break;
16232           default:
16233             return 100;
16234         }
16235       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
16236     }
16237   switch (GET_MODE_SIZE (mode))
16238     {
16239       case 1:
16240         if (in)
16241           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
16242                   : ix86_cost->movzbl_load);
16243         else
16244           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
16245                   : ix86_cost->int_store[0] + 4);
16246         break;
16247       case 2:
16248         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
16249       default:
16250         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
16251         if (mode == TFmode)
16252           mode = XFmode;
16253         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
16254                 * (((int) GET_MODE_SIZE (mode)
16255                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
16256     }
16257 }
16258
16259 /* Compute a (partial) cost for rtx X.  Return true if the complete
16260    cost has been computed, and false if subexpressions should be
16261    scanned.  In either case, *TOTAL contains the cost result.  */
16262
16263 static bool
16264 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
16265 {
16266   enum machine_mode mode = GET_MODE (x);
16267
16268   switch (code)
16269     {
16270     case CONST_INT:
16271     case CONST:
16272     case LABEL_REF:
16273     case SYMBOL_REF:
16274       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
16275         *total = 3;
16276       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
16277         *total = 2;
16278       else if (flag_pic && SYMBOLIC_CONST (x)
16279                && (!TARGET_64BIT
16280                    || (!GET_CODE (x) != LABEL_REF
16281                        && (GET_CODE (x) != SYMBOL_REF
16282                            || !SYMBOL_REF_LOCAL_P (x)))))
16283         *total = 1;
16284       else
16285         *total = 0;
16286       return true;
16287
16288     case CONST_DOUBLE:
16289       if (mode == VOIDmode)
16290         *total = 0;
16291       else
16292         switch (standard_80387_constant_p (x))
16293           {
16294           case 1: /* 0.0 */
16295             *total = 1;
16296             break;
16297           default: /* Other constants */
16298             *total = 2;
16299             break;
16300           case 0:
16301           case -1:
16302             /* Start with (MEM (SYMBOL_REF)), since that's where
16303                it'll probably end up.  Add a penalty for size.  */
16304             *total = (COSTS_N_INSNS (1)
16305                       + (flag_pic != 0 && !TARGET_64BIT)
16306                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
16307             break;
16308           }
16309       return true;
16310
16311     case ZERO_EXTEND:
16312       /* The zero extensions is often completely free on x86_64, so make
16313          it as cheap as possible.  */
16314       if (TARGET_64BIT && mode == DImode
16315           && GET_MODE (XEXP (x, 0)) == SImode)
16316         *total = 1;
16317       else if (TARGET_ZERO_EXTEND_WITH_AND)
16318         *total = COSTS_N_INSNS (ix86_cost->add);
16319       else
16320         *total = COSTS_N_INSNS (ix86_cost->movzx);
16321       return false;
16322
16323     case SIGN_EXTEND:
16324       *total = COSTS_N_INSNS (ix86_cost->movsx);
16325       return false;
16326
16327     case ASHIFT:
16328       if (GET_CODE (XEXP (x, 1)) == CONST_INT
16329           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
16330         {
16331           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
16332           if (value == 1)
16333             {
16334               *total = COSTS_N_INSNS (ix86_cost->add);
16335               return false;
16336             }
16337           if ((value == 2 || value == 3)
16338               && ix86_cost->lea <= ix86_cost->shift_const)
16339             {
16340               *total = COSTS_N_INSNS (ix86_cost->lea);
16341               return false;
16342             }
16343         }
16344       /* FALLTHRU */
16345
16346     case ROTATE:
16347     case ASHIFTRT:
16348     case LSHIFTRT:
16349     case ROTATERT:
16350       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
16351         {
16352           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16353             {
16354               if (INTVAL (XEXP (x, 1)) > 32)
16355                 *total = COSTS_N_INSNS(ix86_cost->shift_const + 2);
16356               else
16357                 *total = COSTS_N_INSNS(ix86_cost->shift_const * 2);
16358             }
16359           else
16360             {
16361               if (GET_CODE (XEXP (x, 1)) == AND)
16362                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 2);
16363               else
16364                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 6 + 2);
16365             }
16366         }
16367       else
16368         {
16369           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16370             *total = COSTS_N_INSNS (ix86_cost->shift_const);
16371           else
16372             *total = COSTS_N_INSNS (ix86_cost->shift_var);
16373         }
16374       return false;
16375
16376     case MULT:
16377       if (FLOAT_MODE_P (mode))
16378         {
16379           *total = COSTS_N_INSNS (ix86_cost->fmul);
16380           return false;
16381         }
16382       else
16383         {
16384           rtx op0 = XEXP (x, 0);
16385           rtx op1 = XEXP (x, 1);
16386           int nbits;
16387           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16388             {
16389               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
16390               for (nbits = 0; value != 0; value &= value - 1)
16391                 nbits++;
16392             }
16393           else
16394             /* This is arbitrary.  */
16395             nbits = 7;
16396
16397           /* Compute costs correctly for widening multiplication.  */
16398           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
16399               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
16400                  == GET_MODE_SIZE (mode))
16401             {
16402               int is_mulwiden = 0;
16403               enum machine_mode inner_mode = GET_MODE (op0);
16404
16405               if (GET_CODE (op0) == GET_CODE (op1))
16406                 is_mulwiden = 1, op1 = XEXP (op1, 0);
16407               else if (GET_CODE (op1) == CONST_INT)
16408                 {
16409                   if (GET_CODE (op0) == SIGN_EXTEND)
16410                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
16411                                   == INTVAL (op1);
16412                   else
16413                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
16414                 }
16415
16416               if (is_mulwiden)
16417                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
16418             }
16419
16420           *total = COSTS_N_INSNS (ix86_cost->mult_init[MODE_INDEX (mode)]
16421                                   + nbits * ix86_cost->mult_bit)
16422                    + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code);
16423
16424           return true;
16425         }
16426
16427     case DIV:
16428     case UDIV:
16429     case MOD:
16430     case UMOD:
16431       if (FLOAT_MODE_P (mode))
16432         *total = COSTS_N_INSNS (ix86_cost->fdiv);
16433       else
16434         *total = COSTS_N_INSNS (ix86_cost->divide[MODE_INDEX (mode)]);
16435       return false;
16436
16437     case PLUS:
16438       if (FLOAT_MODE_P (mode))
16439         *total = COSTS_N_INSNS (ix86_cost->fadd);
16440       else if (GET_MODE_CLASS (mode) == MODE_INT
16441                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
16442         {
16443           if (GET_CODE (XEXP (x, 0)) == PLUS
16444               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
16445               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
16446               && CONSTANT_P (XEXP (x, 1)))
16447             {
16448               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
16449               if (val == 2 || val == 4 || val == 8)
16450                 {
16451                   *total = COSTS_N_INSNS (ix86_cost->lea);
16452                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
16453                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
16454                                       outer_code);
16455                   *total += rtx_cost (XEXP (x, 1), outer_code);
16456                   return true;
16457                 }
16458             }
16459           else if (GET_CODE (XEXP (x, 0)) == MULT
16460                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
16461             {
16462               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
16463               if (val == 2 || val == 4 || val == 8)
16464                 {
16465                   *total = COSTS_N_INSNS (ix86_cost->lea);
16466                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
16467                   *total += rtx_cost (XEXP (x, 1), outer_code);
16468                   return true;
16469                 }
16470             }
16471           else if (GET_CODE (XEXP (x, 0)) == PLUS)
16472             {
16473               *total = COSTS_N_INSNS (ix86_cost->lea);
16474               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
16475               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
16476               *total += rtx_cost (XEXP (x, 1), outer_code);
16477               return true;
16478             }
16479         }
16480       /* FALLTHRU */
16481
16482     case MINUS:
16483       if (FLOAT_MODE_P (mode))
16484         {
16485           *total = COSTS_N_INSNS (ix86_cost->fadd);
16486           return false;
16487         }
16488       /* FALLTHRU */
16489
16490     case AND:
16491     case IOR:
16492     case XOR:
16493       if (!TARGET_64BIT && mode == DImode)
16494         {
16495           *total = (COSTS_N_INSNS (ix86_cost->add) * 2
16496                     + (rtx_cost (XEXP (x, 0), outer_code)
16497                        << (GET_MODE (XEXP (x, 0)) != DImode))
16498                     + (rtx_cost (XEXP (x, 1), outer_code)
16499                        << (GET_MODE (XEXP (x, 1)) != DImode)));
16500           return true;
16501         }
16502       /* FALLTHRU */
16503
16504     case NEG:
16505       if (FLOAT_MODE_P (mode))
16506         {
16507           *total = COSTS_N_INSNS (ix86_cost->fchs);
16508           return false;
16509         }
16510       /* FALLTHRU */
16511
16512     case NOT:
16513       if (!TARGET_64BIT && mode == DImode)
16514         *total = COSTS_N_INSNS (ix86_cost->add * 2);
16515       else
16516         *total = COSTS_N_INSNS (ix86_cost->add);
16517       return false;
16518
16519     case COMPARE:
16520       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
16521           && XEXP (XEXP (x, 0), 1) == const1_rtx
16522           && GET_CODE (XEXP (XEXP (x, 0), 2)) == CONST_INT
16523           && XEXP (x, 1) == const0_rtx)
16524         {
16525           /* This kind of construct is implemented using test[bwl].
16526              Treat it as if we had an AND.  */
16527           *total = (COSTS_N_INSNS (ix86_cost->add)
16528                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
16529                     + rtx_cost (const1_rtx, outer_code));
16530           return true;
16531         }
16532       return false;
16533
16534     case FLOAT_EXTEND:
16535       if (!TARGET_SSE_MATH
16536           || mode == XFmode
16537           || (mode == DFmode && !TARGET_SSE2))
16538         *total = 0;
16539       return false;
16540
16541     case ABS:
16542       if (FLOAT_MODE_P (mode))
16543         *total = COSTS_N_INSNS (ix86_cost->fabs);
16544       return false;
16545
16546     case SQRT:
16547       if (FLOAT_MODE_P (mode))
16548         *total = COSTS_N_INSNS (ix86_cost->fsqrt);
16549       return false;
16550
16551     case UNSPEC:
16552       if (XINT (x, 1) == UNSPEC_TP)
16553         *total = 0;
16554       return false;
16555
16556     default:
16557       return false;
16558     }
16559 }
16560
16561 #if TARGET_MACHO
16562
16563 static int current_machopic_label_num;
16564
16565 /* Given a symbol name and its associated stub, write out the
16566    definition of the stub.  */
16567
16568 void
16569 machopic_output_stub (FILE *file, const char *symb, const char *stub)
16570 {
16571   unsigned int length;
16572   char *binder_name, *symbol_name, lazy_ptr_name[32];
16573   int label = ++current_machopic_label_num;
16574
16575   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
16576   symb = (*targetm.strip_name_encoding) (symb);
16577
16578   length = strlen (stub);
16579   binder_name = alloca (length + 32);
16580   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
16581
16582   length = strlen (symb);
16583   symbol_name = alloca (length + 32);
16584   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
16585
16586   sprintf (lazy_ptr_name, "L%d$lz", label);
16587
16588   if (MACHOPIC_PURE)
16589     switch_to_section (machopic_picsymbol_stub_section);
16590   else
16591     switch_to_section (machopic_symbol_stub_section);
16592
16593   fprintf (file, "%s:\n", stub);
16594   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
16595
16596   if (MACHOPIC_PURE)
16597     {
16598       fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%eax\n", label, label);
16599       fprintf (file, "\tmovl %s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
16600       fprintf (file, "\tjmp %%edx\n");
16601     }
16602   else
16603     fprintf (file, "\tjmp *%s\n", lazy_ptr_name);
16604
16605   fprintf (file, "%s:\n", binder_name);
16606
16607   if (MACHOPIC_PURE)
16608     {
16609       fprintf (file, "\tlea %s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
16610       fprintf (file, "\tpushl %%eax\n");
16611     }
16612   else
16613     fprintf (file, "\t pushl $%s\n", lazy_ptr_name);
16614
16615   fprintf (file, "\tjmp dyld_stub_binding_helper\n");
16616
16617   switch_to_section (machopic_lazy_symbol_ptr_section);
16618   fprintf (file, "%s:\n", lazy_ptr_name);
16619   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
16620   fprintf (file, "\t.long %s\n", binder_name);
16621 }
16622 #endif /* TARGET_MACHO */
16623
16624 /* Order the registers for register allocator.  */
16625
16626 void
16627 x86_order_regs_for_local_alloc (void)
16628 {
16629    int pos = 0;
16630    int i;
16631
16632    /* First allocate the local general purpose registers.  */
16633    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
16634      if (GENERAL_REGNO_P (i) && call_used_regs[i])
16635         reg_alloc_order [pos++] = i;
16636
16637    /* Global general purpose registers.  */
16638    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
16639      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
16640         reg_alloc_order [pos++] = i;
16641
16642    /* x87 registers come first in case we are doing FP math
16643       using them.  */
16644    if (!TARGET_SSE_MATH)
16645      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
16646        reg_alloc_order [pos++] = i;
16647
16648    /* SSE registers.  */
16649    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
16650      reg_alloc_order [pos++] = i;
16651    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
16652      reg_alloc_order [pos++] = i;
16653
16654    /* x87 registers.  */
16655    if (TARGET_SSE_MATH)
16656      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
16657        reg_alloc_order [pos++] = i;
16658
16659    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
16660      reg_alloc_order [pos++] = i;
16661
16662    /* Initialize the rest of array as we do not allocate some registers
16663       at all.  */
16664    while (pos < FIRST_PSEUDO_REGISTER)
16665      reg_alloc_order [pos++] = 0;
16666 }
16667
16668 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
16669    struct attribute_spec.handler.  */
16670 static tree
16671 ix86_handle_struct_attribute (tree *node, tree name,
16672                               tree args ATTRIBUTE_UNUSED,
16673                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
16674 {
16675   tree *type = NULL;
16676   if (DECL_P (*node))
16677     {
16678       if (TREE_CODE (*node) == TYPE_DECL)
16679         type = &TREE_TYPE (*node);
16680     }
16681   else
16682     type = node;
16683
16684   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
16685                  || TREE_CODE (*type) == UNION_TYPE)))
16686     {
16687       warning (OPT_Wattributes, "%qs attribute ignored",
16688                IDENTIFIER_POINTER (name));
16689       *no_add_attrs = true;
16690     }
16691
16692   else if ((is_attribute_p ("ms_struct", name)
16693             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
16694            || ((is_attribute_p ("gcc_struct", name)
16695                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
16696     {
16697       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
16698                IDENTIFIER_POINTER (name));
16699       *no_add_attrs = true;
16700     }
16701
16702   return NULL_TREE;
16703 }
16704
16705 static bool
16706 ix86_ms_bitfield_layout_p (tree record_type)
16707 {
16708   return (TARGET_MS_BITFIELD_LAYOUT &&
16709           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
16710     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
16711 }
16712
16713 /* Returns an expression indicating where the this parameter is
16714    located on entry to the FUNCTION.  */
16715
16716 static rtx
16717 x86_this_parameter (tree function)
16718 {
16719   tree type = TREE_TYPE (function);
16720
16721   if (TARGET_64BIT)
16722     {
16723       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
16724       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
16725     }
16726
16727   if (ix86_function_regparm (type, function) > 0)
16728     {
16729       tree parm;
16730
16731       parm = TYPE_ARG_TYPES (type);
16732       /* Figure out whether or not the function has a variable number of
16733          arguments.  */
16734       for (; parm; parm = TREE_CHAIN (parm))
16735         if (TREE_VALUE (parm) == void_type_node)
16736           break;
16737       /* If not, the this parameter is in the first argument.  */
16738       if (parm)
16739         {
16740           int regno = 0;
16741           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
16742             regno = 2;
16743           return gen_rtx_REG (SImode, regno);
16744         }
16745     }
16746
16747   if (aggregate_value_p (TREE_TYPE (type), type))
16748     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
16749   else
16750     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
16751 }
16752
16753 /* Determine whether x86_output_mi_thunk can succeed.  */
16754
16755 static bool
16756 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
16757                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
16758                          HOST_WIDE_INT vcall_offset, tree function)
16759 {
16760   /* 64-bit can handle anything.  */
16761   if (TARGET_64BIT)
16762     return true;
16763
16764   /* For 32-bit, everything's fine if we have one free register.  */
16765   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
16766     return true;
16767
16768   /* Need a free register for vcall_offset.  */
16769   if (vcall_offset)
16770     return false;
16771
16772   /* Need a free register for GOT references.  */
16773   if (flag_pic && !(*targetm.binds_local_p) (function))
16774     return false;
16775
16776   /* Otherwise ok.  */
16777   return true;
16778 }
16779
16780 /* Output the assembler code for a thunk function.  THUNK_DECL is the
16781    declaration for the thunk function itself, FUNCTION is the decl for
16782    the target function.  DELTA is an immediate constant offset to be
16783    added to THIS.  If VCALL_OFFSET is nonzero, the word at
16784    *(*this + vcall_offset) should be added to THIS.  */
16785
16786 static void
16787 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
16788                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
16789                      HOST_WIDE_INT vcall_offset, tree function)
16790 {
16791   rtx xops[3];
16792   rtx this = x86_this_parameter (function);
16793   rtx this_reg, tmp;
16794
16795   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
16796      pull it in now and let DELTA benefit.  */
16797   if (REG_P (this))
16798     this_reg = this;
16799   else if (vcall_offset)
16800     {
16801       /* Put the this parameter into %eax.  */
16802       xops[0] = this;
16803       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
16804       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
16805     }
16806   else
16807     this_reg = NULL_RTX;
16808
16809   /* Adjust the this parameter by a fixed constant.  */
16810   if (delta)
16811     {
16812       xops[0] = GEN_INT (delta);
16813       xops[1] = this_reg ? this_reg : this;
16814       if (TARGET_64BIT)
16815         {
16816           if (!x86_64_general_operand (xops[0], DImode))
16817             {
16818               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
16819               xops[1] = tmp;
16820               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
16821               xops[0] = tmp;
16822               xops[1] = this;
16823             }
16824           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
16825         }
16826       else
16827         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
16828     }
16829
16830   /* Adjust the this parameter by a value stored in the vtable.  */
16831   if (vcall_offset)
16832     {
16833       if (TARGET_64BIT)
16834         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
16835       else
16836         {
16837           int tmp_regno = 2 /* ECX */;
16838           if (lookup_attribute ("fastcall",
16839               TYPE_ATTRIBUTES (TREE_TYPE (function))))
16840             tmp_regno = 0 /* EAX */;
16841           tmp = gen_rtx_REG (SImode, tmp_regno);
16842         }
16843
16844       xops[0] = gen_rtx_MEM (Pmode, this_reg);
16845       xops[1] = tmp;
16846       if (TARGET_64BIT)
16847         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
16848       else
16849         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
16850
16851       /* Adjust the this parameter.  */
16852       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
16853       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
16854         {
16855           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
16856           xops[0] = GEN_INT (vcall_offset);
16857           xops[1] = tmp2;
16858           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
16859           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
16860         }
16861       xops[1] = this_reg;
16862       if (TARGET_64BIT)
16863         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
16864       else
16865         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
16866     }
16867
16868   /* If necessary, drop THIS back to its stack slot.  */
16869   if (this_reg && this_reg != this)
16870     {
16871       xops[0] = this_reg;
16872       xops[1] = this;
16873       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
16874     }
16875
16876   xops[0] = XEXP (DECL_RTL (function), 0);
16877   if (TARGET_64BIT)
16878     {
16879       if (!flag_pic || (*targetm.binds_local_p) (function))
16880         output_asm_insn ("jmp\t%P0", xops);
16881       else
16882         {
16883           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
16884           tmp = gen_rtx_CONST (Pmode, tmp);
16885           tmp = gen_rtx_MEM (QImode, tmp);
16886           xops[0] = tmp;
16887           output_asm_insn ("jmp\t%A0", xops);
16888         }
16889     }
16890   else
16891     {
16892       if (!flag_pic || (*targetm.binds_local_p) (function))
16893         output_asm_insn ("jmp\t%P0", xops);
16894       else
16895 #if TARGET_MACHO
16896         if (TARGET_MACHO)
16897           {
16898             rtx sym_ref = XEXP (DECL_RTL (function), 0);
16899             tmp = (gen_rtx_SYMBOL_REF
16900                    (Pmode,
16901                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
16902             tmp = gen_rtx_MEM (QImode, tmp);
16903             xops[0] = tmp;
16904             output_asm_insn ("jmp\t%0", xops);
16905           }
16906         else
16907 #endif /* TARGET_MACHO */
16908         {
16909           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
16910           output_set_got (tmp);
16911
16912           xops[1] = tmp;
16913           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
16914           output_asm_insn ("jmp\t{*}%1", xops);
16915         }
16916     }
16917 }
16918
16919 static void
16920 x86_file_start (void)
16921 {
16922   default_file_start ();
16923   if (X86_FILE_START_VERSION_DIRECTIVE)
16924     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
16925   if (X86_FILE_START_FLTUSED)
16926     fputs ("\t.global\t__fltused\n", asm_out_file);
16927   if (ix86_asm_dialect == ASM_INTEL)
16928     fputs ("\t.intel_syntax\n", asm_out_file);
16929 }
16930
16931 int
16932 x86_field_alignment (tree field, int computed)
16933 {
16934   enum machine_mode mode;
16935   tree type = TREE_TYPE (field);
16936
16937   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
16938     return computed;
16939   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
16940                     ? get_inner_array_type (type) : type);
16941   if (mode == DFmode || mode == DCmode
16942       || GET_MODE_CLASS (mode) == MODE_INT
16943       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
16944     return MIN (32, computed);
16945   return computed;
16946 }
16947
16948 /* Output assembler code to FILE to increment profiler label # LABELNO
16949    for profiling a function entry.  */
16950 void
16951 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
16952 {
16953   if (TARGET_64BIT)
16954     if (flag_pic)
16955       {
16956 #ifndef NO_PROFILE_COUNTERS
16957         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
16958 #endif
16959         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
16960       }
16961     else
16962       {
16963 #ifndef NO_PROFILE_COUNTERS
16964         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
16965 #endif
16966         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
16967       }
16968   else if (flag_pic)
16969     {
16970 #ifndef NO_PROFILE_COUNTERS
16971       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
16972                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
16973 #endif
16974       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
16975     }
16976   else
16977     {
16978 #ifndef NO_PROFILE_COUNTERS
16979       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
16980                PROFILE_COUNT_REGISTER);
16981 #endif
16982       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
16983     }
16984 }
16985
16986 /* We don't have exact information about the insn sizes, but we may assume
16987    quite safely that we are informed about all 1 byte insns and memory
16988    address sizes.  This is enough to eliminate unnecessary padding in
16989    99% of cases.  */
16990
16991 static int
16992 min_insn_size (rtx insn)
16993 {
16994   int l = 0;
16995
16996   if (!INSN_P (insn) || !active_insn_p (insn))
16997     return 0;
16998
16999   /* Discard alignments we've emit and jump instructions.  */
17000   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
17001       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
17002     return 0;
17003   if (GET_CODE (insn) == JUMP_INSN
17004       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
17005           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
17006     return 0;
17007
17008   /* Important case - calls are always 5 bytes.
17009      It is common to have many calls in the row.  */
17010   if (GET_CODE (insn) == CALL_INSN
17011       && symbolic_reference_mentioned_p (PATTERN (insn))
17012       && !SIBLING_CALL_P (insn))
17013     return 5;
17014   if (get_attr_length (insn) <= 1)
17015     return 1;
17016
17017   /* For normal instructions we may rely on the sizes of addresses
17018      and the presence of symbol to require 4 bytes of encoding.
17019      This is not the case for jumps where references are PC relative.  */
17020   if (GET_CODE (insn) != JUMP_INSN)
17021     {
17022       l = get_attr_length_address (insn);
17023       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
17024         l = 4;
17025     }
17026   if (l)
17027     return 1+l;
17028   else
17029     return 2;
17030 }
17031
17032 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
17033    window.  */
17034
17035 static void
17036 ix86_avoid_jump_misspredicts (void)
17037 {
17038   rtx insn, start = get_insns ();
17039   int nbytes = 0, njumps = 0;
17040   int isjump = 0;
17041
17042   /* Look for all minimal intervals of instructions containing 4 jumps.
17043      The intervals are bounded by START and INSN.  NBYTES is the total
17044      size of instructions in the interval including INSN and not including
17045      START.  When the NBYTES is smaller than 16 bytes, it is possible
17046      that the end of START and INSN ends up in the same 16byte page.
17047
17048      The smallest offset in the page INSN can start is the case where START
17049      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
17050      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
17051      */
17052   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17053     {
17054
17055       nbytes += min_insn_size (insn);
17056       if (dump_file)
17057         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
17058                 INSN_UID (insn), min_insn_size (insn));
17059       if ((GET_CODE (insn) == JUMP_INSN
17060            && GET_CODE (PATTERN (insn)) != ADDR_VEC
17061            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
17062           || GET_CODE (insn) == CALL_INSN)
17063         njumps++;
17064       else
17065         continue;
17066
17067       while (njumps > 3)
17068         {
17069           start = NEXT_INSN (start);
17070           if ((GET_CODE (start) == JUMP_INSN
17071                && GET_CODE (PATTERN (start)) != ADDR_VEC
17072                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
17073               || GET_CODE (start) == CALL_INSN)
17074             njumps--, isjump = 1;
17075           else
17076             isjump = 0;
17077           nbytes -= min_insn_size (start);
17078         }
17079       gcc_assert (njumps >= 0);
17080       if (dump_file)
17081         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
17082                 INSN_UID (start), INSN_UID (insn), nbytes);
17083
17084       if (njumps == 3 && isjump && nbytes < 16)
17085         {
17086           int padsize = 15 - nbytes + min_insn_size (insn);
17087
17088           if (dump_file)
17089             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
17090                      INSN_UID (insn), padsize);
17091           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
17092         }
17093     }
17094 }
17095
17096 /* AMD Athlon works faster
17097    when RET is not destination of conditional jump or directly preceded
17098    by other jump instruction.  We avoid the penalty by inserting NOP just
17099    before the RET instructions in such cases.  */
17100 static void
17101 ix86_pad_returns (void)
17102 {
17103   edge e;
17104   edge_iterator ei;
17105
17106   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
17107     {
17108       basic_block bb = e->src;
17109       rtx ret = BB_END (bb);
17110       rtx prev;
17111       bool replace = false;
17112
17113       if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
17114           || !maybe_hot_bb_p (bb))
17115         continue;
17116       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
17117         if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
17118           break;
17119       if (prev && GET_CODE (prev) == CODE_LABEL)
17120         {
17121           edge e;
17122           edge_iterator ei;
17123
17124           FOR_EACH_EDGE (e, ei, bb->preds)
17125             if (EDGE_FREQUENCY (e) && e->src->index >= 0
17126                 && !(e->flags & EDGE_FALLTHRU))
17127               replace = true;
17128         }
17129       if (!replace)
17130         {
17131           prev = prev_active_insn (ret);
17132           if (prev
17133               && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
17134                   || GET_CODE (prev) == CALL_INSN))
17135             replace = true;
17136           /* Empty functions get branch mispredict even when the jump destination
17137              is not visible to us.  */
17138           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
17139             replace = true;
17140         }
17141       if (replace)
17142         {
17143           emit_insn_before (gen_return_internal_long (), ret);
17144           delete_insn (ret);
17145         }
17146     }
17147 }
17148
17149 /* Implement machine specific optimizations.  We implement padding of returns
17150    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
17151 static void
17152 ix86_reorg (void)
17153 {
17154   if (TARGET_ATHLON_K8 && optimize && !optimize_size)
17155     ix86_pad_returns ();
17156   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
17157     ix86_avoid_jump_misspredicts ();
17158 }
17159
17160 /* Return nonzero when QImode register that must be represented via REX prefix
17161    is used.  */
17162 bool
17163 x86_extended_QIreg_mentioned_p (rtx insn)
17164 {
17165   int i;
17166   extract_insn_cached (insn);
17167   for (i = 0; i < recog_data.n_operands; i++)
17168     if (REG_P (recog_data.operand[i])
17169         && REGNO (recog_data.operand[i]) >= 4)
17170        return true;
17171   return false;
17172 }
17173
17174 /* Return nonzero when P points to register encoded via REX prefix.
17175    Called via for_each_rtx.  */
17176 static int
17177 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
17178 {
17179    unsigned int regno;
17180    if (!REG_P (*p))
17181      return 0;
17182    regno = REGNO (*p);
17183    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
17184 }
17185
17186 /* Return true when INSN mentions register that must be encoded using REX
17187    prefix.  */
17188 bool
17189 x86_extended_reg_mentioned_p (rtx insn)
17190 {
17191   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
17192 }
17193
17194 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
17195    optabs would emit if we didn't have TFmode patterns.  */
17196
17197 void
17198 x86_emit_floatuns (rtx operands[2])
17199 {
17200   rtx neglab, donelab, i0, i1, f0, in, out;
17201   enum machine_mode mode, inmode;
17202
17203   inmode = GET_MODE (operands[1]);
17204   gcc_assert (inmode == SImode || inmode == DImode);
17205
17206   out = operands[0];
17207   in = force_reg (inmode, operands[1]);
17208   mode = GET_MODE (out);
17209   neglab = gen_label_rtx ();
17210   donelab = gen_label_rtx ();
17211   i1 = gen_reg_rtx (Pmode);
17212   f0 = gen_reg_rtx (mode);
17213
17214   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
17215
17216   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
17217   emit_jump_insn (gen_jump (donelab));
17218   emit_barrier ();
17219
17220   emit_label (neglab);
17221
17222   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
17223   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
17224   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
17225   expand_float (f0, i0, 0);
17226   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
17227
17228   emit_label (donelab);
17229 }
17230 \f
17231 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
17232    with all elements equal to VAR.  Return true if successful.  */
17233
17234 static bool
17235 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
17236                                    rtx target, rtx val)
17237 {
17238   enum machine_mode smode, wsmode, wvmode;
17239   rtx x;
17240
17241   switch (mode)
17242     {
17243     case V2SImode:
17244     case V2SFmode:
17245       if (!mmx_ok && !TARGET_SSE)
17246         return false;
17247       /* FALLTHRU */
17248
17249     case V2DFmode:
17250     case V2DImode:
17251     case V4SFmode:
17252     case V4SImode:
17253       val = force_reg (GET_MODE_INNER (mode), val);
17254       x = gen_rtx_VEC_DUPLICATE (mode, val);
17255       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17256       return true;
17257
17258     case V4HImode:
17259       if (!mmx_ok)
17260         return false;
17261       if (TARGET_SSE || TARGET_3DNOW_A)
17262         {
17263           val = gen_lowpart (SImode, val);
17264           x = gen_rtx_TRUNCATE (HImode, val);
17265           x = gen_rtx_VEC_DUPLICATE (mode, x);
17266           emit_insn (gen_rtx_SET (VOIDmode, target, x));
17267           return true;
17268         }
17269       else
17270         {
17271           smode = HImode;
17272           wsmode = SImode;
17273           wvmode = V2SImode;
17274           goto widen;
17275         }
17276
17277     case V8QImode:
17278       if (!mmx_ok)
17279         return false;
17280       smode = QImode;
17281       wsmode = HImode;
17282       wvmode = V4HImode;
17283       goto widen;
17284     case V8HImode:
17285       smode = HImode;
17286       wsmode = SImode;
17287       wvmode = V4SImode;
17288       goto widen;
17289     case V16QImode:
17290       smode = QImode;
17291       wsmode = HImode;
17292       wvmode = V8HImode;
17293       goto widen;
17294     widen:
17295       /* Replicate the value once into the next wider mode and recurse.  */
17296       val = convert_modes (wsmode, smode, val, true);
17297       x = expand_simple_binop (wsmode, ASHIFT, val,
17298                                GEN_INT (GET_MODE_BITSIZE (smode)),
17299                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
17300       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
17301
17302       x = gen_reg_rtx (wvmode);
17303       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
17304         gcc_unreachable ();
17305       emit_move_insn (target, gen_lowpart (mode, x));
17306       return true;
17307
17308     default:
17309       return false;
17310     }
17311 }
17312
17313 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
17314    whose low element is VAR, and other elements are zero.  Return true
17315    if successful.  */
17316
17317 static bool
17318 ix86_expand_vector_init_low_nonzero (bool mmx_ok, enum machine_mode mode,
17319                                      rtx target, rtx var)
17320 {
17321   enum machine_mode vsimode;
17322   rtx x;
17323
17324   switch (mode)
17325     {
17326     case V2SFmode:
17327     case V2SImode:
17328       if (!mmx_ok && !TARGET_SSE)
17329         return false;
17330       /* FALLTHRU */
17331
17332     case V2DFmode:
17333     case V2DImode:
17334       var = force_reg (GET_MODE_INNER (mode), var);
17335       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
17336       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17337       return true;
17338
17339     case V4SFmode:
17340     case V4SImode:
17341       var = force_reg (GET_MODE_INNER (mode), var);
17342       x = gen_rtx_VEC_DUPLICATE (mode, var);
17343       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
17344       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17345       return true;
17346
17347     case V8HImode:
17348     case V16QImode:
17349       vsimode = V4SImode;
17350       goto widen;
17351     case V4HImode:
17352     case V8QImode:
17353       if (!mmx_ok)
17354         return false;
17355       vsimode = V2SImode;
17356       goto widen;
17357     widen:
17358       /* Zero extend the variable element to SImode and recurse.  */
17359       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
17360
17361       x = gen_reg_rtx (vsimode);
17362       if (!ix86_expand_vector_init_low_nonzero (mmx_ok, vsimode, x, var))
17363         gcc_unreachable ();
17364
17365       emit_move_insn (target, gen_lowpart (mode, x));
17366       return true;
17367
17368     default:
17369       return false;
17370     }
17371 }
17372
17373 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
17374    consisting of the values in VALS.  It is known that all elements
17375    except ONE_VAR are constants.  Return true if successful.  */
17376
17377 static bool
17378 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
17379                                  rtx target, rtx vals, int one_var)
17380 {
17381   rtx var = XVECEXP (vals, 0, one_var);
17382   enum machine_mode wmode;
17383   rtx const_vec, x;
17384
17385   const_vec = copy_rtx (vals);
17386   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
17387   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
17388
17389   switch (mode)
17390     {
17391     case V2DFmode:
17392     case V2DImode:
17393     case V2SFmode:
17394     case V2SImode:
17395       /* For the two element vectors, it's just as easy to use
17396          the general case.  */
17397       return false;
17398
17399     case V4SFmode:
17400     case V4SImode:
17401     case V8HImode:
17402     case V4HImode:
17403       break;
17404
17405     case V16QImode:
17406       wmode = V8HImode;
17407       goto widen;
17408     case V8QImode:
17409       wmode = V4HImode;
17410       goto widen;
17411     widen:
17412       /* There's no way to set one QImode entry easily.  Combine
17413          the variable value with its adjacent constant value, and
17414          promote to an HImode set.  */
17415       x = XVECEXP (vals, 0, one_var ^ 1);
17416       if (one_var & 1)
17417         {
17418           var = convert_modes (HImode, QImode, var, true);
17419           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
17420                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
17421           x = GEN_INT (INTVAL (x) & 0xff);
17422         }
17423       else
17424         {
17425           var = convert_modes (HImode, QImode, var, true);
17426           x = gen_int_mode (INTVAL (x) << 8, HImode);
17427         }
17428       if (x != const0_rtx)
17429         var = expand_simple_binop (HImode, IOR, var, x, var,
17430                                    1, OPTAB_LIB_WIDEN);
17431
17432       x = gen_reg_rtx (wmode);
17433       emit_move_insn (x, gen_lowpart (wmode, const_vec));
17434       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
17435
17436       emit_move_insn (target, gen_lowpart (mode, x));
17437       return true;
17438
17439     default:
17440       return false;
17441     }
17442
17443   emit_move_insn (target, const_vec);
17444   ix86_expand_vector_set (mmx_ok, target, var, one_var);
17445   return true;
17446 }
17447
17448 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
17449    all values variable, and none identical.  */
17450
17451 static void
17452 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
17453                                  rtx target, rtx vals)
17454 {
17455   enum machine_mode half_mode = GET_MODE_INNER (mode);
17456   rtx op0 = NULL, op1 = NULL;
17457   bool use_vec_concat = false;
17458
17459   switch (mode)
17460     {
17461     case V2SFmode:
17462     case V2SImode:
17463       if (!mmx_ok && !TARGET_SSE)
17464         break;
17465       /* FALLTHRU */
17466
17467     case V2DFmode:
17468     case V2DImode:
17469       /* For the two element vectors, we always implement VEC_CONCAT.  */
17470       op0 = XVECEXP (vals, 0, 0);
17471       op1 = XVECEXP (vals, 0, 1);
17472       use_vec_concat = true;
17473       break;
17474
17475     case V4SFmode:
17476       half_mode = V2SFmode;
17477       goto half;
17478     case V4SImode:
17479       half_mode = V2SImode;
17480       goto half;
17481     half:
17482       {
17483         rtvec v;
17484
17485         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
17486            Recurse to load the two halves.  */
17487
17488         op0 = gen_reg_rtx (half_mode);
17489         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
17490         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
17491
17492         op1 = gen_reg_rtx (half_mode);
17493         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
17494         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
17495
17496         use_vec_concat = true;
17497       }
17498       break;
17499
17500     case V8HImode:
17501     case V16QImode:
17502     case V4HImode:
17503     case V8QImode:
17504       break;
17505
17506     default:
17507       gcc_unreachable ();
17508     }
17509
17510   if (use_vec_concat)
17511     {
17512       if (!register_operand (op0, half_mode))
17513         op0 = force_reg (half_mode, op0);
17514       if (!register_operand (op1, half_mode))
17515         op1 = force_reg (half_mode, op1);
17516
17517       emit_insn (gen_rtx_SET (VOIDmode, target, 
17518                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
17519     }
17520   else
17521     {
17522       int i, j, n_elts, n_words, n_elt_per_word;
17523       enum machine_mode inner_mode;
17524       rtx words[4], shift;
17525
17526       inner_mode = GET_MODE_INNER (mode);
17527       n_elts = GET_MODE_NUNITS (mode);
17528       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
17529       n_elt_per_word = n_elts / n_words;
17530       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
17531
17532       for (i = 0; i < n_words; ++i)
17533         {
17534           rtx word = NULL_RTX;
17535
17536           for (j = 0; j < n_elt_per_word; ++j)
17537             {
17538               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
17539               elt = convert_modes (word_mode, inner_mode, elt, true);
17540
17541               if (j == 0)
17542                 word = elt;
17543               else
17544                 {
17545                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
17546                                               word, 1, OPTAB_LIB_WIDEN);
17547                   word = expand_simple_binop (word_mode, IOR, word, elt,
17548                                               word, 1, OPTAB_LIB_WIDEN);
17549                 }
17550             }
17551
17552           words[i] = word;
17553         }
17554
17555       if (n_words == 1)
17556         emit_move_insn (target, gen_lowpart (mode, words[0]));
17557       else if (n_words == 2)
17558         {
17559           rtx tmp = gen_reg_rtx (mode);
17560           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
17561           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
17562           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
17563           emit_move_insn (target, tmp);
17564         }
17565       else if (n_words == 4)
17566         {
17567           rtx tmp = gen_reg_rtx (V4SImode);
17568           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
17569           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
17570           emit_move_insn (target, gen_lowpart (mode, tmp));
17571         }
17572       else
17573         gcc_unreachable ();
17574     }
17575 }
17576
17577 /* Initialize vector TARGET via VALS.  Suppress the use of MMX 
17578    instructions unless MMX_OK is true.  */
17579
17580 void
17581 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
17582 {
17583   enum machine_mode mode = GET_MODE (target);
17584   enum machine_mode inner_mode = GET_MODE_INNER (mode);
17585   int n_elts = GET_MODE_NUNITS (mode);
17586   int n_var = 0, one_var = -1;
17587   bool all_same = true, all_const_zero = true;
17588   int i;
17589   rtx x;
17590
17591   for (i = 0; i < n_elts; ++i)
17592     {
17593       x = XVECEXP (vals, 0, i);
17594       if (!CONSTANT_P (x))
17595         n_var++, one_var = i;
17596       else if (x != CONST0_RTX (inner_mode))
17597         all_const_zero = false;
17598       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
17599         all_same = false;
17600     }
17601
17602   /* Constants are best loaded from the constant pool.  */
17603   if (n_var == 0)
17604     {
17605       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
17606       return;
17607     }
17608
17609   /* If all values are identical, broadcast the value.  */
17610   if (all_same
17611       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
17612                                             XVECEXP (vals, 0, 0)))
17613     return;
17614
17615   /* Values where only one field is non-constant are best loaded from
17616      the pool and overwritten via move later.  */
17617   if (n_var == 1)
17618     {
17619       if (all_const_zero && one_var == 0
17620           && ix86_expand_vector_init_low_nonzero (mmx_ok, mode, target,
17621                                                   XVECEXP (vals, 0, 0)))
17622         return;
17623
17624       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
17625         return;
17626     }
17627
17628   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
17629 }
17630
17631 void
17632 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
17633 {
17634   enum machine_mode mode = GET_MODE (target);
17635   enum machine_mode inner_mode = GET_MODE_INNER (mode);
17636   bool use_vec_merge = false;
17637   rtx tmp;
17638
17639   switch (mode)
17640     {
17641     case V2SFmode:
17642     case V2SImode:
17643       if (mmx_ok)
17644         {
17645           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
17646           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
17647           if (elt == 0)
17648             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
17649           else
17650             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
17651           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
17652           return;
17653         }
17654       break;
17655
17656     case V2DFmode:
17657     case V2DImode:
17658       {
17659         rtx op0, op1;
17660
17661         /* For the two element vectors, we implement a VEC_CONCAT with
17662            the extraction of the other element.  */
17663
17664         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
17665         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
17666
17667         if (elt == 0)
17668           op0 = val, op1 = tmp;
17669         else
17670           op0 = tmp, op1 = val;
17671
17672         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
17673         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
17674       }
17675       return;
17676
17677     case V4SFmode:
17678       switch (elt)
17679         {
17680         case 0:
17681           use_vec_merge = true;
17682           break;
17683
17684         case 1:
17685           /* tmp = target = A B C D */
17686           tmp = copy_to_reg (target);
17687           /* target = A A B B */
17688           emit_insn (gen_sse_unpcklps (target, target, target));
17689           /* target = X A B B */
17690           ix86_expand_vector_set (false, target, val, 0);
17691           /* target = A X C D  */
17692           emit_insn (gen_sse_shufps_1 (target, target, tmp,
17693                                        GEN_INT (1), GEN_INT (0),
17694                                        GEN_INT (2+4), GEN_INT (3+4)));
17695           return;
17696
17697         case 2:
17698           /* tmp = target = A B C D */
17699           tmp = copy_to_reg (target);
17700           /* tmp = X B C D */
17701           ix86_expand_vector_set (false, tmp, val, 0);
17702           /* target = A B X D */
17703           emit_insn (gen_sse_shufps_1 (target, target, tmp,
17704                                        GEN_INT (0), GEN_INT (1),
17705                                        GEN_INT (0+4), GEN_INT (3+4)));
17706           return;
17707
17708         case 3:
17709           /* tmp = target = A B C D */
17710           tmp = copy_to_reg (target);
17711           /* tmp = X B C D */
17712           ix86_expand_vector_set (false, tmp, val, 0);
17713           /* target = A B X D */
17714           emit_insn (gen_sse_shufps_1 (target, target, tmp,
17715                                        GEN_INT (0), GEN_INT (1),
17716                                        GEN_INT (2+4), GEN_INT (0+4)));
17717           return;
17718
17719         default:
17720           gcc_unreachable ();
17721         }
17722       break;
17723
17724     case V4SImode:
17725       /* Element 0 handled by vec_merge below.  */
17726       if (elt == 0)
17727         {
17728           use_vec_merge = true;
17729           break;
17730         }
17731
17732       if (TARGET_SSE2)
17733         {
17734           /* With SSE2, use integer shuffles to swap element 0 and ELT,
17735              store into element 0, then shuffle them back.  */
17736
17737           rtx order[4];
17738
17739           order[0] = GEN_INT (elt);
17740           order[1] = const1_rtx;
17741           order[2] = const2_rtx;
17742           order[3] = GEN_INT (3);
17743           order[elt] = const0_rtx;
17744
17745           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
17746                                         order[1], order[2], order[3]));
17747
17748           ix86_expand_vector_set (false, target, val, 0);
17749
17750           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
17751                                         order[1], order[2], order[3]));
17752         }
17753       else
17754         {
17755           /* For SSE1, we have to reuse the V4SF code.  */
17756           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
17757                                   gen_lowpart (SFmode, val), elt);
17758         }
17759       return;
17760
17761     case V8HImode:
17762       use_vec_merge = TARGET_SSE2;
17763       break;
17764     case V4HImode:
17765       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
17766       break;
17767
17768     case V16QImode:
17769     case V8QImode:
17770     default:
17771       break;
17772     }
17773
17774   if (use_vec_merge)
17775     {
17776       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
17777       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
17778       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
17779     }
17780   else
17781     {
17782       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
17783
17784       emit_move_insn (mem, target);
17785
17786       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
17787       emit_move_insn (tmp, val);
17788
17789       emit_move_insn (target, mem);
17790     }
17791 }
17792
17793 void
17794 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
17795 {
17796   enum machine_mode mode = GET_MODE (vec);
17797   enum machine_mode inner_mode = GET_MODE_INNER (mode);
17798   bool use_vec_extr = false;
17799   rtx tmp;
17800
17801   switch (mode)
17802     {
17803     case V2SImode:
17804     case V2SFmode:
17805       if (!mmx_ok)
17806         break;
17807       /* FALLTHRU */
17808
17809     case V2DFmode:
17810     case V2DImode:
17811       use_vec_extr = true;
17812       break;
17813
17814     case V4SFmode:
17815       switch (elt)
17816         {
17817         case 0:
17818           tmp = vec;
17819           break;
17820
17821         case 1:
17822         case 3:
17823           tmp = gen_reg_rtx (mode);
17824           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
17825                                        GEN_INT (elt), GEN_INT (elt),
17826                                        GEN_INT (elt+4), GEN_INT (elt+4)));
17827           break;
17828
17829         case 2:
17830           tmp = gen_reg_rtx (mode);
17831           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
17832           break;
17833
17834         default:
17835           gcc_unreachable ();
17836         }
17837       vec = tmp;
17838       use_vec_extr = true;
17839       elt = 0;
17840       break;
17841
17842     case V4SImode:
17843       if (TARGET_SSE2)
17844         {
17845           switch (elt)
17846             {
17847             case 0:
17848               tmp = vec;
17849               break;
17850
17851             case 1:
17852             case 3:
17853               tmp = gen_reg_rtx (mode);
17854               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
17855                                             GEN_INT (elt), GEN_INT (elt),
17856                                             GEN_INT (elt), GEN_INT (elt)));
17857               break;
17858
17859             case 2:
17860               tmp = gen_reg_rtx (mode);
17861               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
17862               break;
17863
17864             default:
17865               gcc_unreachable ();
17866             }
17867           vec = tmp;
17868           use_vec_extr = true;
17869           elt = 0;
17870         }
17871       else
17872         {
17873           /* For SSE1, we have to reuse the V4SF code.  */
17874           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
17875                                       gen_lowpart (V4SFmode, vec), elt);
17876           return;
17877         }
17878       break;
17879
17880     case V8HImode:
17881       use_vec_extr = TARGET_SSE2;
17882       break;
17883     case V4HImode:
17884       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
17885       break;
17886
17887     case V16QImode:
17888     case V8QImode:
17889       /* ??? Could extract the appropriate HImode element and shift.  */
17890     default:
17891       break;
17892     }
17893
17894   if (use_vec_extr)
17895     {
17896       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
17897       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
17898
17899       /* Let the rtl optimizers know about the zero extension performed.  */
17900       if (inner_mode == HImode)
17901         {
17902           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
17903           target = gen_lowpart (SImode, target);
17904         }
17905
17906       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
17907     }
17908   else
17909     {
17910       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
17911
17912       emit_move_insn (mem, vec);
17913
17914       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
17915       emit_move_insn (target, tmp);
17916     }
17917 }
17918
17919 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
17920    pattern to reduce; DEST is the destination; IN is the input vector.  */
17921
17922 void
17923 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
17924 {
17925   rtx tmp1, tmp2, tmp3;
17926
17927   tmp1 = gen_reg_rtx (V4SFmode);
17928   tmp2 = gen_reg_rtx (V4SFmode);
17929   tmp3 = gen_reg_rtx (V4SFmode);
17930
17931   emit_insn (gen_sse_movhlps (tmp1, in, in));
17932   emit_insn (fn (tmp2, tmp1, in));
17933
17934   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
17935                                GEN_INT (1), GEN_INT (1),
17936                                GEN_INT (1+4), GEN_INT (1+4)));
17937   emit_insn (fn (dest, tmp2, tmp3));
17938 }
17939 \f
17940 /* Implements target hook vector_mode_supported_p.  */
17941 static bool
17942 ix86_vector_mode_supported_p (enum machine_mode mode)
17943 {
17944   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
17945     return true;
17946   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
17947     return true;
17948   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
17949     return true;
17950   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
17951     return true;
17952   return false;
17953 }
17954
17955 /* Worker function for TARGET_MD_ASM_CLOBBERS.
17956
17957    We do this in the new i386 backend to maintain source compatibility
17958    with the old cc0-based compiler.  */
17959
17960 static tree
17961 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
17962                       tree inputs ATTRIBUTE_UNUSED,
17963                       tree clobbers)
17964 {
17965   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
17966                         clobbers);
17967   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
17968                         clobbers);
17969   clobbers = tree_cons (NULL_TREE, build_string (7, "dirflag"),
17970                         clobbers);
17971   return clobbers;
17972 }
17973
17974 /* Return true if this goes in small data/bss.  */
17975
17976 static bool
17977 ix86_in_large_data_p (tree exp)
17978 {
17979   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
17980     return false;
17981
17982   /* Functions are never large data.  */
17983   if (TREE_CODE (exp) == FUNCTION_DECL)
17984     return false;
17985
17986   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
17987     {
17988       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
17989       if (strcmp (section, ".ldata") == 0
17990           || strcmp (section, ".lbss") == 0)
17991         return true;
17992       return false;
17993     }
17994   else
17995     {
17996       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
17997
17998       /* If this is an incomplete type with size 0, then we can't put it
17999          in data because it might be too big when completed.  */
18000       if (!size || size > ix86_section_threshold)
18001         return true;
18002     }
18003
18004   return false;
18005 }
18006 static void
18007 ix86_encode_section_info (tree decl, rtx rtl, int first)
18008 {
18009   default_encode_section_info (decl, rtl, first);
18010
18011   if (TREE_CODE (decl) == VAR_DECL
18012       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
18013       && ix86_in_large_data_p (decl))
18014     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
18015 }
18016
18017 /* Worker function for REVERSE_CONDITION.  */
18018
18019 enum rtx_code
18020 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
18021 {
18022   return (mode != CCFPmode && mode != CCFPUmode
18023           ? reverse_condition (code)
18024           : reverse_condition_maybe_unordered (code));
18025 }
18026
18027 /* Output code to perform an x87 FP register move, from OPERANDS[1]
18028    to OPERANDS[0].  */
18029
18030 const char *
18031 output_387_reg_move (rtx insn, rtx *operands)
18032 {
18033   if (REG_P (operands[1])
18034       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
18035     {
18036       if (REGNO (operands[0]) == FIRST_STACK_REG
18037           && TARGET_USE_FFREEP)
18038         return "ffreep\t%y0";
18039       return "fstp\t%y0";
18040     }
18041   if (STACK_TOP_P (operands[0]))
18042     return "fld%z1\t%y1";
18043   return "fst\t%y0";
18044 }
18045
18046 /* Output code to perform a conditional jump to LABEL, if C2 flag in
18047    FP status register is set.  */
18048
18049 void
18050 ix86_emit_fp_unordered_jump (rtx label)
18051 {
18052   rtx reg = gen_reg_rtx (HImode);
18053   rtx temp;
18054
18055   emit_insn (gen_x86_fnstsw_1 (reg));
18056
18057   if (TARGET_USE_SAHF)
18058     {
18059       emit_insn (gen_x86_sahf_1 (reg));
18060
18061       temp = gen_rtx_REG (CCmode, FLAGS_REG);
18062       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
18063     }
18064   else
18065     {
18066       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
18067
18068       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
18069       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
18070     }
18071
18072   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
18073                               gen_rtx_LABEL_REF (VOIDmode, label),
18074                               pc_rtx);
18075   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
18076   emit_jump_insn (temp);
18077 }
18078
18079 /* Output code to perform a log1p XFmode calculation.  */
18080
18081 void ix86_emit_i387_log1p (rtx op0, rtx op1)
18082 {
18083   rtx label1 = gen_label_rtx ();
18084   rtx label2 = gen_label_rtx ();
18085
18086   rtx tmp = gen_reg_rtx (XFmode);
18087   rtx tmp2 = gen_reg_rtx (XFmode);
18088
18089   emit_insn (gen_absxf2 (tmp, op1));
18090   emit_insn (gen_cmpxf (tmp,
18091     CONST_DOUBLE_FROM_REAL_VALUE (
18092        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
18093        XFmode)));
18094   emit_jump_insn (gen_bge (label1));
18095
18096   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
18097   emit_insn (gen_fyl2xp1_xf3 (op0, tmp2, op1));
18098   emit_jump (label2);
18099
18100   emit_label (label1);
18101   emit_move_insn (tmp, CONST1_RTX (XFmode));
18102   emit_insn (gen_addxf3 (tmp, op1, tmp));
18103   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
18104   emit_insn (gen_fyl2x_xf3 (op0, tmp2, tmp));
18105
18106   emit_label (label2);
18107 }
18108
18109 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
18110
18111 static void
18112 i386_solaris_elf_named_section (const char *name, unsigned int flags,
18113                                 tree decl)
18114 {
18115   /* With Binutils 2.15, the "@unwind" marker must be specified on
18116      every occurrence of the ".eh_frame" section, not just the first
18117      one.  */
18118   if (TARGET_64BIT
18119       && strcmp (name, ".eh_frame") == 0)
18120     {
18121       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
18122                flags & SECTION_WRITE ? "aw" : "a");
18123       return;
18124     }
18125   default_elf_asm_named_section (name, flags, decl);
18126 }
18127
18128 /* Return the mangling of TYPE if it is an extended fundamental type.  */
18129
18130 static const char *
18131 ix86_mangle_fundamental_type (tree type)
18132 {
18133   switch (TYPE_MODE (type))
18134     {
18135     case TFmode:
18136       /* __float128 is "g".  */
18137       return "g";
18138     case XFmode:
18139       /* "long double" or __float80 is "e".  */
18140       return "e";
18141     default:
18142       return NULL;
18143     }
18144 }
18145
18146 /* For 32-bit code we can save PIC register setup by using
18147    __stack_chk_fail_local hidden function instead of calling
18148    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
18149    register, so it is better to call __stack_chk_fail directly.  */
18150
18151 static tree
18152 ix86_stack_protect_fail (void)
18153 {
18154   return TARGET_64BIT
18155          ? default_external_stack_protect_fail ()
18156          : default_hidden_stack_protect_fail ();
18157 }
18158
18159 /* Select a format to encode pointers in exception handling data.  CODE
18160    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
18161    true if the symbol may be affected by dynamic relocations.
18162
18163    ??? All x86 object file formats are capable of representing this.
18164    After all, the relocation needed is the same as for the call insn.
18165    Whether or not a particular assembler allows us to enter such, I
18166    guess we'll have to see.  */
18167 int
18168 asm_preferred_eh_data_format (int code, int global)
18169 {
18170   if (flag_pic)
18171     {
18172 int type = DW_EH_PE_sdata8;
18173       if (!TARGET_64BIT
18174           || ix86_cmodel == CM_SMALL_PIC
18175           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
18176         type = DW_EH_PE_sdata4;
18177       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
18178     }
18179   if (ix86_cmodel == CM_SMALL
18180       || (ix86_cmodel == CM_MEDIUM && code))
18181     return DW_EH_PE_udata4;
18182   return DW_EH_PE_absptr;
18183 }
18184
18185 #include "gt-i386.h"