OSDN Git Service

4a8695275051342251b4ae48290c1966e9e7718e
[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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-codes.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "except.h"
39 #include "function.h"
40 #include "recog.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "langhooks.h"
49 #include "cgraph.h"
50 #include "tree-gimple.h"
51
52 #ifndef CHECK_STACK_LIMIT
53 #define CHECK_STACK_LIMIT (-1)
54 #endif
55
56 /* Return index of given mode in mult and division cost tables.  */
57 #define MODE_INDEX(mode)                                        \
58   ((mode) == QImode ? 0                                         \
59    : (mode) == HImode ? 1                                       \
60    : (mode) == SImode ? 2                                       \
61    : (mode) == DImode ? 3                                       \
62    : 4)
63
64 /* Processor costs (relative to an add) */
65 static const
66 struct processor_costs size_cost = {    /* costs for tunning for size */
67   2,                                    /* cost of an add instruction */
68   3,                                    /* cost of a lea instruction */
69   2,                                    /* variable shift costs */
70   3,                                    /* constant shift costs */
71   {3, 3, 3, 3, 5},                      /* cost of starting a multiply */
72   0,                                    /* cost of multiply per each bit set */
73   {3, 3, 3, 3, 5},                      /* cost of a divide/mod */
74   3,                                    /* cost of movsx */
75   3,                                    /* cost of movzx */
76   0,                                    /* "large" insn */
77   2,                                    /* MOVE_RATIO */
78   2,                                    /* cost for loading QImode using movzbl */
79   {2, 2, 2},                            /* cost of loading integer registers
80                                            in QImode, HImode and SImode.
81                                            Relative to reg-reg move (2).  */
82   {2, 2, 2},                            /* cost of storing integer registers */
83   2,                                    /* cost of reg,reg fld/fst */
84   {2, 2, 2},                            /* cost of loading fp registers
85                                            in SFmode, DFmode and XFmode */
86   {2, 2, 2},                            /* cost of loading integer registers */
87   3,                                    /* cost of moving MMX register */
88   {3, 3},                               /* cost of loading MMX registers
89                                            in SImode and DImode */
90   {3, 3},                               /* cost of storing MMX registers
91                                            in SImode and DImode */
92   3,                                    /* cost of moving SSE register */
93   {3, 3, 3},                            /* cost of loading SSE registers
94                                            in SImode, DImode and TImode */
95   {3, 3, 3},                            /* cost of storing SSE registers
96                                            in SImode, DImode and TImode */
97   3,                                    /* MMX or SSE register to integer */
98   0,                                    /* size of prefetch block */
99   0,                                    /* number of parallel prefetches */
100   1,                                    /* Branch cost */
101   2,                                    /* cost of FADD and FSUB insns.  */
102   2,                                    /* cost of FMUL instruction.  */
103   2,                                    /* cost of FDIV instruction.  */
104   2,                                    /* cost of FABS instruction.  */
105   2,                                    /* cost of FCHS instruction.  */
106   2,                                    /* cost of FSQRT instruction.  */
107 };
108
109 /* Processor costs (relative to an add) */
110 static const
111 struct processor_costs i386_cost = {    /* 386 specific costs */
112   1,                                    /* cost of an add instruction */
113   1,                                    /* cost of a lea instruction */
114   3,                                    /* variable shift costs */
115   2,                                    /* constant shift costs */
116   {6, 6, 6, 6, 6},                      /* cost of starting a multiply */
117   1,                                    /* cost of multiply per each bit set */
118   {23, 23, 23, 23, 23},                 /* cost of a divide/mod */
119   3,                                    /* cost of movsx */
120   2,                                    /* cost of movzx */
121   15,                                   /* "large" insn */
122   3,                                    /* MOVE_RATIO */
123   4,                                    /* cost for loading QImode using movzbl */
124   {2, 4, 2},                            /* cost of loading integer registers
125                                            in QImode, HImode and SImode.
126                                            Relative to reg-reg move (2).  */
127   {2, 4, 2},                            /* cost of storing integer registers */
128   2,                                    /* cost of reg,reg fld/fst */
129   {8, 8, 8},                            /* cost of loading fp registers
130                                            in SFmode, DFmode and XFmode */
131   {8, 8, 8},                            /* cost of loading integer registers */
132   2,                                    /* cost of moving MMX register */
133   {4, 8},                               /* cost of loading MMX registers
134                                            in SImode and DImode */
135   {4, 8},                               /* cost of storing MMX registers
136                                            in SImode and DImode */
137   2,                                    /* cost of moving SSE register */
138   {4, 8, 16},                           /* cost of loading SSE registers
139                                            in SImode, DImode and TImode */
140   {4, 8, 16},                           /* cost of storing SSE registers
141                                            in SImode, DImode and TImode */
142   3,                                    /* MMX or SSE register to integer */
143   0,                                    /* size of prefetch block */
144   0,                                    /* number of parallel prefetches */
145   1,                                    /* Branch cost */
146   23,                                   /* cost of FADD and FSUB insns.  */
147   27,                                   /* cost of FMUL instruction.  */
148   88,                                   /* cost of FDIV instruction.  */
149   22,                                   /* cost of FABS instruction.  */
150   24,                                   /* cost of FCHS instruction.  */
151   122,                                  /* cost of FSQRT instruction.  */
152 };
153
154 static const
155 struct processor_costs i486_cost = {    /* 486 specific costs */
156   1,                                    /* cost of an add instruction */
157   1,                                    /* cost of a lea instruction */
158   3,                                    /* variable shift costs */
159   2,                                    /* constant shift costs */
160   {12, 12, 12, 12, 12},                 /* cost of starting a multiply */
161   1,                                    /* cost of multiply per each bit set */
162   {40, 40, 40, 40, 40},                 /* cost of a divide/mod */
163   3,                                    /* cost of movsx */
164   2,                                    /* cost of movzx */
165   15,                                   /* "large" insn */
166   3,                                    /* MOVE_RATIO */
167   4,                                    /* cost for loading QImode using movzbl */
168   {2, 4, 2},                            /* cost of loading integer registers
169                                            in QImode, HImode and SImode.
170                                            Relative to reg-reg move (2).  */
171   {2, 4, 2},                            /* cost of storing integer registers */
172   2,                                    /* cost of reg,reg fld/fst */
173   {8, 8, 8},                            /* cost of loading fp registers
174                                            in SFmode, DFmode and XFmode */
175   {8, 8, 8},                            /* cost of loading integer registers */
176   2,                                    /* cost of moving MMX register */
177   {4, 8},                               /* cost of loading MMX registers
178                                            in SImode and DImode */
179   {4, 8},                               /* cost of storing MMX registers
180                                            in SImode and DImode */
181   2,                                    /* cost of moving SSE register */
182   {4, 8, 16},                           /* cost of loading SSE registers
183                                            in SImode, DImode and TImode */
184   {4, 8, 16},                           /* cost of storing SSE registers
185                                            in SImode, DImode and TImode */
186   3,                                    /* MMX or SSE register to integer */
187   0,                                    /* size of prefetch block */
188   0,                                    /* number of parallel prefetches */
189   1,                                    /* Branch cost */
190   8,                                    /* cost of FADD and FSUB insns.  */
191   16,                                   /* cost of FMUL instruction.  */
192   73,                                   /* cost of FDIV instruction.  */
193   3,                                    /* cost of FABS instruction.  */
194   3,                                    /* cost of FCHS instruction.  */
195   83,                                   /* cost of FSQRT instruction.  */
196 };
197
198 static const
199 struct processor_costs pentium_cost = {
200   1,                                    /* cost of an add instruction */
201   1,                                    /* cost of a lea instruction */
202   4,                                    /* variable shift costs */
203   1,                                    /* constant shift costs */
204   {11, 11, 11, 11, 11},                 /* cost of starting a multiply */
205   0,                                    /* cost of multiply per each bit set */
206   {25, 25, 25, 25, 25},                 /* cost of a divide/mod */
207   3,                                    /* cost of movsx */
208   2,                                    /* cost of movzx */
209   8,                                    /* "large" insn */
210   6,                                    /* MOVE_RATIO */
211   6,                                    /* cost for loading QImode using movzbl */
212   {2, 4, 2},                            /* cost of loading integer registers
213                                            in QImode, HImode and SImode.
214                                            Relative to reg-reg move (2).  */
215   {2, 4, 2},                            /* cost of storing integer registers */
216   2,                                    /* cost of reg,reg fld/fst */
217   {2, 2, 6},                            /* cost of loading fp registers
218                                            in SFmode, DFmode and XFmode */
219   {4, 4, 6},                            /* cost of loading integer registers */
220   8,                                    /* cost of moving MMX register */
221   {8, 8},                               /* cost of loading MMX registers
222                                            in SImode and DImode */
223   {8, 8},                               /* cost of storing MMX registers
224                                            in SImode and DImode */
225   2,                                    /* cost of moving SSE register */
226   {4, 8, 16},                           /* cost of loading SSE registers
227                                            in SImode, DImode and TImode */
228   {4, 8, 16},                           /* cost of storing SSE registers
229                                            in SImode, DImode and TImode */
230   3,                                    /* MMX or SSE register to integer */
231   0,                                    /* size of prefetch block */
232   0,                                    /* number of parallel prefetches */
233   2,                                    /* Branch cost */
234   3,                                    /* cost of FADD and FSUB insns.  */
235   3,                                    /* cost of FMUL instruction.  */
236   39,                                   /* cost of FDIV instruction.  */
237   1,                                    /* cost of FABS instruction.  */
238   1,                                    /* cost of FCHS instruction.  */
239   70,                                   /* cost of FSQRT instruction.  */
240 };
241
242 static const
243 struct processor_costs pentiumpro_cost = {
244   1,                                    /* cost of an add instruction */
245   1,                                    /* cost of a lea instruction */
246   1,                                    /* variable shift costs */
247   1,                                    /* constant shift costs */
248   {4, 4, 4, 4, 4},                      /* cost of starting a multiply */
249   0,                                    /* cost of multiply per each bit set */
250   {17, 17, 17, 17, 17},                 /* cost of a divide/mod */
251   1,                                    /* cost of movsx */
252   1,                                    /* cost of movzx */
253   8,                                    /* "large" insn */
254   6,                                    /* MOVE_RATIO */
255   2,                                    /* cost for loading QImode using movzbl */
256   {4, 4, 4},                            /* cost of loading integer registers
257                                            in QImode, HImode and SImode.
258                                            Relative to reg-reg move (2).  */
259   {2, 2, 2},                            /* cost of storing integer registers */
260   2,                                    /* cost of reg,reg fld/fst */
261   {2, 2, 6},                            /* cost of loading fp registers
262                                            in SFmode, DFmode and XFmode */
263   {4, 4, 6},                            /* cost of loading integer registers */
264   2,                                    /* cost of moving MMX register */
265   {2, 2},                               /* cost of loading MMX registers
266                                            in SImode and DImode */
267   {2, 2},                               /* cost of storing MMX registers
268                                            in SImode and DImode */
269   2,                                    /* cost of moving SSE register */
270   {2, 2, 8},                            /* cost of loading SSE registers
271                                            in SImode, DImode and TImode */
272   {2, 2, 8},                            /* cost of storing SSE registers
273                                            in SImode, DImode and TImode */
274   3,                                    /* MMX or SSE register to integer */
275   32,                                   /* size of prefetch block */
276   6,                                    /* number of parallel prefetches */
277   2,                                    /* Branch cost */
278   3,                                    /* cost of FADD and FSUB insns.  */
279   5,                                    /* cost of FMUL instruction.  */
280   56,                                   /* cost of FDIV instruction.  */
281   2,                                    /* cost of FABS instruction.  */
282   2,                                    /* cost of FCHS instruction.  */
283   56,                                   /* cost of FSQRT instruction.  */
284 };
285
286 static const
287 struct processor_costs k6_cost = {
288   1,                                    /* cost of an add instruction */
289   2,                                    /* cost of a lea instruction */
290   1,                                    /* variable shift costs */
291   1,                                    /* constant shift costs */
292   {3, 3, 3, 3, 3},                      /* cost of starting a multiply */
293   0,                                    /* cost of multiply per each bit set */
294   {18, 18, 18, 18, 18},                 /* cost of a divide/mod */
295   2,                                    /* cost of movsx */
296   2,                                    /* cost of movzx */
297   8,                                    /* "large" insn */
298   4,                                    /* MOVE_RATIO */
299   3,                                    /* cost for loading QImode using movzbl */
300   {4, 5, 4},                            /* cost of loading integer registers
301                                            in QImode, HImode and SImode.
302                                            Relative to reg-reg move (2).  */
303   {2, 3, 2},                            /* cost of storing integer registers */
304   4,                                    /* cost of reg,reg fld/fst */
305   {6, 6, 6},                            /* cost of loading fp registers
306                                            in SFmode, DFmode and XFmode */
307   {4, 4, 4},                            /* cost of loading integer registers */
308   2,                                    /* cost of moving MMX register */
309   {2, 2},                               /* cost of loading MMX registers
310                                            in SImode and DImode */
311   {2, 2},                               /* cost of storing MMX registers
312                                            in SImode and DImode */
313   2,                                    /* cost of moving SSE register */
314   {2, 2, 8},                            /* cost of loading SSE registers
315                                            in SImode, DImode and TImode */
316   {2, 2, 8},                            /* cost of storing SSE registers
317                                            in SImode, DImode and TImode */
318   6,                                    /* MMX or SSE register to integer */
319   32,                                   /* size of prefetch block */
320   1,                                    /* number of parallel prefetches */
321   1,                                    /* Branch cost */
322   2,                                    /* cost of FADD and FSUB insns.  */
323   2,                                    /* cost of FMUL instruction.  */
324   56,                                   /* cost of FDIV instruction.  */
325   2,                                    /* cost of FABS instruction.  */
326   2,                                    /* cost of FCHS instruction.  */
327   56,                                   /* cost of FSQRT instruction.  */
328 };
329
330 static const
331 struct processor_costs athlon_cost = {
332   1,                                    /* cost of an add instruction */
333   2,                                    /* cost of a lea instruction */
334   1,                                    /* variable shift costs */
335   1,                                    /* constant shift costs */
336   {5, 5, 5, 5, 5},                      /* cost of starting a multiply */
337   0,                                    /* cost of multiply per each bit set */
338   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
339   1,                                    /* cost of movsx */
340   1,                                    /* cost of movzx */
341   8,                                    /* "large" insn */
342   9,                                    /* MOVE_RATIO */
343   4,                                    /* cost for loading QImode using movzbl */
344   {3, 4, 3},                            /* cost of loading integer registers
345                                            in QImode, HImode and SImode.
346                                            Relative to reg-reg move (2).  */
347   {3, 4, 3},                            /* cost of storing integer registers */
348   4,                                    /* cost of reg,reg fld/fst */
349   {4, 4, 12},                           /* cost of loading fp registers
350                                            in SFmode, DFmode and XFmode */
351   {6, 6, 8},                            /* cost of loading integer registers */
352   2,                                    /* cost of moving MMX register */
353   {4, 4},                               /* cost of loading MMX registers
354                                            in SImode and DImode */
355   {4, 4},                               /* cost of storing MMX registers
356                                            in SImode and DImode */
357   2,                                    /* cost of moving SSE register */
358   {4, 4, 6},                            /* cost of loading SSE registers
359                                            in SImode, DImode and TImode */
360   {4, 4, 5},                            /* cost of storing SSE registers
361                                            in SImode, DImode and TImode */
362   5,                                    /* MMX or SSE register to integer */
363   64,                                   /* size of prefetch block */
364   6,                                    /* number of parallel prefetches */
365   2,                                    /* Branch cost */
366   4,                                    /* cost of FADD and FSUB insns.  */
367   4,                                    /* cost of FMUL instruction.  */
368   24,                                   /* cost of FDIV instruction.  */
369   2,                                    /* cost of FABS instruction.  */
370   2,                                    /* cost of FCHS instruction.  */
371   35,                                   /* cost of FSQRT instruction.  */
372 };
373
374 static const
375 struct processor_costs k8_cost = {
376   1,                                    /* cost of an add instruction */
377   2,                                    /* cost of a lea instruction */
378   1,                                    /* variable shift costs */
379   1,                                    /* constant shift costs */
380   {3, 4, 3, 4, 5},                      /* cost of starting a multiply */
381   0,                                    /* cost of multiply per each bit set */
382   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
383   1,                                    /* cost of movsx */
384   1,                                    /* cost of movzx */
385   8,                                    /* "large" insn */
386   9,                                    /* MOVE_RATIO */
387   4,                                    /* cost for loading QImode using movzbl */
388   {3, 4, 3},                            /* cost of loading integer registers
389                                            in QImode, HImode and SImode.
390                                            Relative to reg-reg move (2).  */
391   {3, 4, 3},                            /* cost of storing integer registers */
392   4,                                    /* cost of reg,reg fld/fst */
393   {4, 4, 12},                           /* cost of loading fp registers
394                                            in SFmode, DFmode and XFmode */
395   {6, 6, 8},                            /* cost of loading integer registers */
396   2,                                    /* cost of moving MMX register */
397   {3, 3},                               /* cost of loading MMX registers
398                                            in SImode and DImode */
399   {4, 4},                               /* cost of storing MMX registers
400                                            in SImode and DImode */
401   2,                                    /* cost of moving SSE register */
402   {4, 3, 6},                            /* cost of loading SSE registers
403                                            in SImode, DImode and TImode */
404   {4, 4, 5},                            /* cost of storing SSE registers
405                                            in SImode, DImode and TImode */
406   5,                                    /* MMX or SSE register to integer */
407   64,                                   /* size of prefetch block */
408   6,                                    /* number of parallel prefetches */
409   2,                                    /* Branch cost */
410   4,                                    /* cost of FADD and FSUB insns.  */
411   4,                                    /* cost of FMUL instruction.  */
412   19,                                   /* cost of FDIV instruction.  */
413   2,                                    /* cost of FABS instruction.  */
414   2,                                    /* cost of FCHS instruction.  */
415   35,                                   /* cost of FSQRT instruction.  */
416 };
417
418 static const
419 struct processor_costs pentium4_cost = {
420   1,                                    /* cost of an add instruction */
421   3,                                    /* cost of a lea instruction */
422   4,                                    /* variable shift costs */
423   4,                                    /* constant shift costs */
424   {15, 15, 15, 15, 15},                 /* cost of starting a multiply */
425   0,                                    /* cost of multiply per each bit set */
426   {56, 56, 56, 56, 56},                 /* cost of a divide/mod */
427   1,                                    /* cost of movsx */
428   1,                                    /* cost of movzx */
429   16,                                   /* "large" insn */
430   6,                                    /* MOVE_RATIO */
431   2,                                    /* cost for loading QImode using movzbl */
432   {4, 5, 4},                            /* cost of loading integer registers
433                                            in QImode, HImode and SImode.
434                                            Relative to reg-reg move (2).  */
435   {2, 3, 2},                            /* cost of storing integer registers */
436   2,                                    /* cost of reg,reg fld/fst */
437   {2, 2, 6},                            /* cost of loading fp registers
438                                            in SFmode, DFmode and XFmode */
439   {4, 4, 6},                            /* cost of loading integer registers */
440   2,                                    /* cost of moving MMX register */
441   {2, 2},                               /* cost of loading MMX registers
442                                            in SImode and DImode */
443   {2, 2},                               /* cost of storing MMX registers
444                                            in SImode and DImode */
445   12,                                   /* cost of moving SSE register */
446   {12, 12, 12},                         /* cost of loading SSE registers
447                                            in SImode, DImode and TImode */
448   {2, 2, 8},                            /* cost of storing SSE registers
449                                            in SImode, DImode and TImode */
450   10,                                   /* MMX or SSE register to integer */
451   64,                                   /* size of prefetch block */
452   6,                                    /* number of parallel prefetches */
453   2,                                    /* Branch cost */
454   5,                                    /* cost of FADD and FSUB insns.  */
455   7,                                    /* cost of FMUL instruction.  */
456   43,                                   /* cost of FDIV instruction.  */
457   2,                                    /* cost of FABS instruction.  */
458   2,                                    /* cost of FCHS instruction.  */
459   43,                                   /* cost of FSQRT instruction.  */
460 };
461
462 static const
463 struct processor_costs nocona_cost = {
464   1,                                    /* cost of an add instruction */
465   1,                                    /* cost of a lea instruction */
466   1,                                    /* variable shift costs */
467   1,                                    /* constant shift costs */
468   {10, 10, 10, 10, 10},                 /* cost of starting a multiply */
469   0,                                    /* cost of multiply per each bit set */
470   {66, 66, 66, 66, 66},                 /* cost of a divide/mod */
471   1,                                    /* cost of movsx */
472   1,                                    /* cost of movzx */
473   16,                                   /* "large" insn */
474   9,                                    /* MOVE_RATIO */
475   4,                                    /* cost for loading QImode using movzbl */
476   {4, 4, 4},                            /* cost of loading integer registers
477                                            in QImode, HImode and SImode.
478                                            Relative to reg-reg move (2).  */
479   {4, 4, 4},                            /* cost of storing integer registers */
480   3,                                    /* cost of reg,reg fld/fst */
481   {12, 12, 12},                         /* cost of loading fp registers
482                                            in SFmode, DFmode and XFmode */
483   {4, 4, 4},                            /* cost of loading integer registers */
484   6,                                    /* cost of moving MMX register */
485   {12, 12},                             /* cost of loading MMX registers
486                                            in SImode and DImode */
487   {12, 12},                             /* cost of storing MMX registers
488                                            in SImode and DImode */
489   6,                                    /* cost of moving SSE register */
490   {12, 12, 12},                         /* cost of loading SSE registers
491                                            in SImode, DImode and TImode */
492   {12, 12, 12},                         /* cost of storing SSE registers
493                                            in SImode, DImode and TImode */
494   8,                                    /* MMX or SSE register to integer */
495   128,                                  /* size of prefetch block */
496   8,                                    /* number of parallel prefetches */
497   1,                                    /* Branch cost */
498   6,                                    /* cost of FADD and FSUB insns.  */
499   8,                                    /* cost of FMUL instruction.  */
500   40,                                   /* cost of FDIV instruction.  */
501   3,                                    /* cost of FABS instruction.  */
502   3,                                    /* cost of FCHS instruction.  */
503   44,                                   /* cost of FSQRT instruction.  */
504 };
505
506 const struct processor_costs *ix86_cost = &pentium_cost;
507
508 /* Processor feature/optimization bitmasks.  */
509 #define m_386 (1<<PROCESSOR_I386)
510 #define m_486 (1<<PROCESSOR_I486)
511 #define m_PENT (1<<PROCESSOR_PENTIUM)
512 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
513 #define m_K6  (1<<PROCESSOR_K6)
514 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
515 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
516 #define m_K8  (1<<PROCESSOR_K8)
517 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
518 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
519
520 const int x86_use_leave = m_386 | m_K6 | m_ATHLON_K8;
521 const int x86_push_memory = m_386 | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
522 const int x86_zero_extend_with_and = m_486 | m_PENT;
523 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA /* m_386 | m_K6 */;
524 const int x86_double_with_add = ~m_386;
525 const int x86_use_bit_test = m_386;
526 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6;
527 const int x86_cmove = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
528 const int x86_3dnow_a = m_ATHLON_K8;
529 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
530 /* Branch hints were put in P4 based on simulation result. But
531    after P4 was made, no performance benefit was observed with
532    branch hints. It also increases the code size. As the result,
533    icc never generates branch hints.  */
534 const int x86_branch_hints = 0;
535 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4 | m_NOCONA;
536 const int x86_partial_reg_stall = m_PPRO;
537 const int x86_use_loop = m_K6;
538 const int x86_use_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT);
539 const int x86_use_mov0 = m_K6;
540 const int x86_use_cltd = ~(m_PENT | m_K6);
541 const int x86_read_modify_write = ~m_PENT;
542 const int x86_read_modify = ~(m_PENT | m_PPRO);
543 const int x86_split_long_moves = m_PPRO;
544 const int x86_promote_QImode = m_K6 | m_PENT | m_386 | m_486 | m_ATHLON_K8;
545 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
546 const int x86_single_stringop = m_386 | m_PENT4 | m_NOCONA;
547 const int x86_qimode_math = ~(0);
548 const int x86_promote_qi_regs = 0;
549 const int x86_himode_math = ~(m_PPRO);
550 const int x86_promote_hi_regs = m_PPRO;
551 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA;
552 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4 | m_NOCONA;
553 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6 | m_PENT4 | m_NOCONA;
554 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6 | m_386 | m_486 | m_PENT4 | m_NOCONA;
555 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO);
556 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4 | m_NOCONA;
557 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4 | m_NOCONA;
558 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO;
559 const int x86_prologue_using_move = m_ATHLON_K8 | m_PPRO;
560 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PPRO;
561 const int x86_decompose_lea = m_PENT4 | m_NOCONA;
562 const int x86_shift1 = ~m_486;
563 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
564 const int x86_sse_partial_reg_dependency = m_PENT4 | m_NOCONA | m_PPRO;
565 /* Set for machines where the type and dependencies are resolved on SSE register
566    parts instead of whole registers, so we may maintain just lower part of
567    scalar values in proper format leaving the upper part undefined.  */
568 const int x86_sse_partial_regs = m_ATHLON_K8;
569 /* Athlon optimizes partial-register FPS special case, thus avoiding the
570    need for extra instructions beforehand  */
571 const int x86_sse_partial_regs_for_cvtsd2ss = 0;
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 const int x86_inter_unit_moves = ~(m_ATHLON_K8);
577 const int x86_ext_80387_constants = m_K6 | m_ATHLON | m_PENT4 | m_NOCONA | m_PPRO;
578 /* Some CPU cores are not able to predict more than 4 branch instructions in
579    the 16 byte window.  */
580 const int x86_four_jump_limit = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
581
582 /* In case the average insn count for single function invocation is
583    lower than this constant, emit fast (but longer) prologue and
584    epilogue code.  */
585 #define FAST_PROLOGUE_INSN_COUNT 20
586
587 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
588 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
589 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
590 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
591
592 /* Array of the smallest class containing reg number REGNO, indexed by
593    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
594
595 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
596 {
597   /* ax, dx, cx, bx */
598   AREG, DREG, CREG, BREG,
599   /* si, di, bp, sp */
600   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
601   /* FP registers */
602   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
603   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
604   /* arg pointer */
605   NON_Q_REGS,
606   /* flags, fpsr, dirflag, frame */
607   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
608   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
609   SSE_REGS, SSE_REGS,
610   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
611   MMX_REGS, MMX_REGS,
612   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
613   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
614   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
615   SSE_REGS, SSE_REGS,
616 };
617
618 /* The "default" register map used in 32bit mode.  */
619
620 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
621 {
622   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
623   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
624   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
625   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
626   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
627   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
628   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
629 };
630
631 static int const x86_64_int_parameter_registers[6] =
632 {
633   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
634   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
635 };
636
637 static int const x86_64_int_return_registers[4] =
638 {
639   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
640 };
641
642 /* The "default" register map used in 64bit mode.  */
643 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
644 {
645   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
646   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
647   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
648   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
649   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
650   8,9,10,11,12,13,14,15,                /* extended integer registers */
651   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
652 };
653
654 /* Define the register numbers to be used in Dwarf debugging information.
655    The SVR4 reference port C compiler uses the following register numbers
656    in its Dwarf output code:
657         0 for %eax (gcc regno = 0)
658         1 for %ecx (gcc regno = 2)
659         2 for %edx (gcc regno = 1)
660         3 for %ebx (gcc regno = 3)
661         4 for %esp (gcc regno = 7)
662         5 for %ebp (gcc regno = 6)
663         6 for %esi (gcc regno = 4)
664         7 for %edi (gcc regno = 5)
665    The following three DWARF register numbers are never generated by
666    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
667    believes these numbers have these meanings.
668         8  for %eip    (no gcc equivalent)
669         9  for %eflags (gcc regno = 17)
670         10 for %trapno (no gcc equivalent)
671    It is not at all clear how we should number the FP stack registers
672    for the x86 architecture.  If the version of SDB on x86/svr4 were
673    a bit less brain dead with respect to floating-point then we would
674    have a precedent to follow with respect to DWARF register numbers
675    for x86 FP registers, but the SDB on x86/svr4 is so completely
676    broken with respect to FP registers that it is hardly worth thinking
677    of it as something to strive for compatibility with.
678    The version of x86/svr4 SDB I have at the moment does (partially)
679    seem to believe that DWARF register number 11 is associated with
680    the x86 register %st(0), but that's about all.  Higher DWARF
681    register numbers don't seem to be associated with anything in
682    particular, and even for DWARF regno 11, SDB only seems to under-
683    stand that it should say that a variable lives in %st(0) (when
684    asked via an `=' command) if we said it was in DWARF regno 11,
685    but SDB still prints garbage when asked for the value of the
686    variable in question (via a `/' command).
687    (Also note that the labels SDB prints for various FP stack regs
688    when doing an `x' command are all wrong.)
689    Note that these problems generally don't affect the native SVR4
690    C compiler because it doesn't allow the use of -O with -g and
691    because when it is *not* optimizing, it allocates a memory
692    location for each floating-point variable, and the memory
693    location is what gets described in the DWARF AT_location
694    attribute for the variable in question.
695    Regardless of the severe mental illness of the x86/svr4 SDB, we
696    do something sensible here and we use the following DWARF
697    register numbers.  Note that these are all stack-top-relative
698    numbers.
699         11 for %st(0) (gcc regno = 8)
700         12 for %st(1) (gcc regno = 9)
701         13 for %st(2) (gcc regno = 10)
702         14 for %st(3) (gcc regno = 11)
703         15 for %st(4) (gcc regno = 12)
704         16 for %st(5) (gcc regno = 13)
705         17 for %st(6) (gcc regno = 14)
706         18 for %st(7) (gcc regno = 15)
707 */
708 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
709 {
710   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
711   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
712   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, dir, frame */
713   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
714   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
715   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
716   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
717 };
718
719 /* Test and compare insns in i386.md store the information needed to
720    generate branch and scc insns here.  */
721
722 rtx ix86_compare_op0 = NULL_RTX;
723 rtx ix86_compare_op1 = NULL_RTX;
724
725 #define MAX_386_STACK_LOCALS 3
726 /* Size of the register save area.  */
727 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
728
729 /* Define the structure for the machine field in struct function.  */
730
731 struct stack_local_entry GTY(())
732 {
733   unsigned short mode;
734   unsigned short n;
735   rtx rtl;
736   struct stack_local_entry *next;
737 };
738
739 /* Structure describing stack frame layout.
740    Stack grows downward:
741
742    [arguments]
743                                               <- ARG_POINTER
744    saved pc
745
746    saved frame pointer if frame_pointer_needed
747                                               <- HARD_FRAME_POINTER
748    [saved regs]
749
750    [padding1]          \
751                         )
752    [va_arg registers]  (
753                         > to_allocate         <- FRAME_POINTER
754    [frame]             (
755                         )
756    [padding2]          /
757   */
758 struct ix86_frame
759 {
760   int nregs;
761   int padding1;
762   int va_arg_size;
763   HOST_WIDE_INT frame;
764   int padding2;
765   int outgoing_arguments_size;
766   int red_zone_size;
767
768   HOST_WIDE_INT to_allocate;
769   /* The offsets relative to ARG_POINTER.  */
770   HOST_WIDE_INT frame_pointer_offset;
771   HOST_WIDE_INT hard_frame_pointer_offset;
772   HOST_WIDE_INT stack_pointer_offset;
773
774   /* When save_regs_using_mov is set, emit prologue using
775      move instead of push instructions.  */
776   bool save_regs_using_mov;
777 };
778
779 /* Used to enable/disable debugging features.  */
780 const char *ix86_debug_arg_string, *ix86_debug_addr_string;
781 /* Code model option as passed by user.  */
782 const char *ix86_cmodel_string;
783 /* Parsed value.  */
784 enum cmodel ix86_cmodel;
785 /* Asm dialect.  */
786 const char *ix86_asm_string;
787 enum asm_dialect ix86_asm_dialect = ASM_ATT;
788 /* TLS dialext.  */
789 const char *ix86_tls_dialect_string;
790 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
791
792 /* Which unit we are generating floating point math for.  */
793 enum fpmath_unit ix86_fpmath;
794
795 /* Which cpu are we scheduling for.  */
796 enum processor_type ix86_tune;
797 /* Which instruction set architecture to use.  */
798 enum processor_type ix86_arch;
799
800 /* Strings to hold which cpu and instruction set architecture  to use.  */
801 const char *ix86_tune_string;           /* for -mtune=<xxx> */
802 const char *ix86_arch_string;           /* for -march=<xxx> */
803 const char *ix86_fpmath_string;         /* for -mfpmath=<xxx> */
804
805 /* # of registers to use to pass arguments.  */
806 const char *ix86_regparm_string;
807
808 /* true if sse prefetch instruction is not NOOP.  */
809 int x86_prefetch_sse;
810
811 /* ix86_regparm_string as a number */
812 int ix86_regparm;
813
814 /* Alignment to use for loops and jumps:  */
815
816 /* Power of two alignment for loops.  */
817 const char *ix86_align_loops_string;
818
819 /* Power of two alignment for non-loop jumps.  */
820 const char *ix86_align_jumps_string;
821
822 /* Power of two alignment for stack boundary in bytes.  */
823 const char *ix86_preferred_stack_boundary_string;
824
825 /* Preferred alignment for stack boundary in bits.  */
826 unsigned int ix86_preferred_stack_boundary;
827
828 /* Values 1-5: see jump.c */
829 int ix86_branch_cost;
830 const char *ix86_branch_cost_string;
831
832 /* Power of two alignment for functions.  */
833 const char *ix86_align_funcs_string;
834
835 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
836 char internal_label_prefix[16];
837 int internal_label_prefix_len;
838 \f
839 static void output_pic_addr_const (FILE *, rtx, int);
840 static void put_condition_code (enum rtx_code, enum machine_mode,
841                                 int, int, FILE *);
842 static const char *get_some_local_dynamic_name (void);
843 static int get_some_local_dynamic_name_1 (rtx *, void *);
844 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
845 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
846                                                    rtx *);
847 static bool ix86_fixed_condition_code_regs (unsigned int *, unsigned int *);
848 static enum machine_mode ix86_cc_modes_compatible (enum machine_mode,
849                                                    enum machine_mode);
850 static rtx get_thread_pointer (int);
851 static rtx legitimize_tls_address (rtx, enum tls_model, int);
852 static void get_pc_thunk_name (char [32], unsigned int);
853 static rtx gen_push (rtx);
854 static int ix86_flags_dependant (rtx, rtx, enum attr_type);
855 static int ix86_agi_dependant (rtx, rtx, enum attr_type);
856 static struct machine_function * ix86_init_machine_status (void);
857 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
858 static int ix86_nsaved_regs (void);
859 static void ix86_emit_save_regs (void);
860 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
861 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
862 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
863 static HOST_WIDE_INT ix86_GOT_alias_set (void);
864 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
865 static rtx ix86_expand_aligntest (rtx, int);
866 static void ix86_expand_strlensi_unroll_1 (rtx, rtx, rtx);
867 static int ix86_issue_rate (void);
868 static int ix86_adjust_cost (rtx, rtx, rtx, int);
869 static int ia32_multipass_dfa_lookahead (void);
870 static void ix86_init_mmx_sse_builtins (void);
871 static rtx x86_this_parameter (tree);
872 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
873                                  HOST_WIDE_INT, tree);
874 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
875 static void x86_file_start (void);
876 static void ix86_reorg (void);
877 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
878 static tree ix86_build_builtin_va_list (void);
879 static void ix86_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
880                                          tree, int *, int);
881 static tree ix86_gimplify_va_arg (tree, tree, tree *, tree *);
882 static bool ix86_vector_mode_supported_p (enum machine_mode);
883
884 static int ix86_address_cost (rtx);
885 static bool ix86_cannot_force_const_mem (rtx);
886 static rtx ix86_delegitimize_address (rtx);
887
888 struct builtin_description;
889 static rtx ix86_expand_sse_comi (const struct builtin_description *,
890                                  tree, rtx);
891 static rtx ix86_expand_sse_compare (const struct builtin_description *,
892                                     tree, rtx);
893 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
894 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
895 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
896 static rtx ix86_expand_store_builtin (enum insn_code, tree);
897 static rtx safe_vector_operand (rtx, enum machine_mode);
898 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
899 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
900 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
901 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
902 static int ix86_fp_comparison_cost (enum rtx_code code);
903 static unsigned int ix86_select_alt_pic_regnum (void);
904 static int ix86_save_reg (unsigned int, int);
905 static void ix86_compute_frame_layout (struct ix86_frame *);
906 static int ix86_comp_type_attributes (tree, tree);
907 static int ix86_function_regparm (tree, tree);
908 const struct attribute_spec ix86_attribute_table[];
909 static bool ix86_function_ok_for_sibcall (tree, tree);
910 static tree ix86_handle_cdecl_attribute (tree *, tree, tree, int, bool *);
911 static tree ix86_handle_regparm_attribute (tree *, tree, tree, int, bool *);
912 static int ix86_value_regno (enum machine_mode);
913 static bool contains_128bit_aligned_vector_p (tree);
914 static rtx ix86_struct_value_rtx (tree, int);
915 static bool ix86_ms_bitfield_layout_p (tree);
916 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
917 static int extended_reg_mentioned_1 (rtx *, void *);
918 static bool ix86_rtx_costs (rtx, int, int, int *);
919 static int min_insn_size (rtx);
920 static tree ix86_md_asm_clobbers (tree clobbers);
921 static bool ix86_must_pass_in_stack (enum machine_mode mode, tree type);
922 static bool ix86_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
923                                     tree, bool);
924
925 #if defined (DO_GLOBAL_CTORS_BODY) && defined (HAS_INIT_SECTION)
926 static void ix86_svr3_asm_out_constructor (rtx, int);
927 #endif
928
929 /* Register class used for passing given 64bit part of the argument.
930    These represent classes as documented by the PS ABI, with the exception
931    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
932    use SF or DFmode move instead of DImode to avoid reformatting penalties.
933
934    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
935    whenever possible (upper half does contain padding).
936  */
937 enum x86_64_reg_class
938   {
939     X86_64_NO_CLASS,
940     X86_64_INTEGER_CLASS,
941     X86_64_INTEGERSI_CLASS,
942     X86_64_SSE_CLASS,
943     X86_64_SSESF_CLASS,
944     X86_64_SSEDF_CLASS,
945     X86_64_SSEUP_CLASS,
946     X86_64_X87_CLASS,
947     X86_64_X87UP_CLASS,
948     X86_64_MEMORY_CLASS
949   };
950 static const char * const x86_64_reg_class_name[] =
951    {"no", "integer", "integerSI", "sse", "sseSF", "sseDF", "sseup", "x87", "x87up", "no"};
952
953 #define MAX_CLASSES 4
954 static int classify_argument (enum machine_mode, tree,
955                               enum x86_64_reg_class [MAX_CLASSES], int);
956 static int examine_argument (enum machine_mode, tree, int, int *, int *);
957 static rtx construct_container (enum machine_mode, tree, int, int, int,
958                                 const int *, int);
959 static enum x86_64_reg_class merge_classes (enum x86_64_reg_class,
960                                             enum x86_64_reg_class);
961
962 /* Table of constants used by fldpi, fldln2, etc....  */
963 static REAL_VALUE_TYPE ext_80387_constants_table [5];
964 static bool ext_80387_constants_init = 0;
965 static void init_ext_80387_constants (void);
966 \f
967 /* Initialize the GCC target structure.  */
968 #undef TARGET_ATTRIBUTE_TABLE
969 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
970 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
971 #  undef TARGET_MERGE_DECL_ATTRIBUTES
972 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
973 #endif
974
975 #undef TARGET_COMP_TYPE_ATTRIBUTES
976 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
977
978 #undef TARGET_INIT_BUILTINS
979 #define TARGET_INIT_BUILTINS ix86_init_builtins
980
981 #undef TARGET_EXPAND_BUILTIN
982 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
983
984 #undef TARGET_ASM_FUNCTION_EPILOGUE
985 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
986
987 #undef TARGET_ASM_OPEN_PAREN
988 #define TARGET_ASM_OPEN_PAREN ""
989 #undef TARGET_ASM_CLOSE_PAREN
990 #define TARGET_ASM_CLOSE_PAREN ""
991
992 #undef TARGET_ASM_ALIGNED_HI_OP
993 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
994 #undef TARGET_ASM_ALIGNED_SI_OP
995 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
996 #ifdef ASM_QUAD
997 #undef TARGET_ASM_ALIGNED_DI_OP
998 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
999 #endif
1000
1001 #undef TARGET_ASM_UNALIGNED_HI_OP
1002 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
1003 #undef TARGET_ASM_UNALIGNED_SI_OP
1004 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
1005 #undef TARGET_ASM_UNALIGNED_DI_OP
1006 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
1007
1008 #undef TARGET_SCHED_ADJUST_COST
1009 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
1010 #undef TARGET_SCHED_ISSUE_RATE
1011 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
1012 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1013 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1014   ia32_multipass_dfa_lookahead
1015
1016 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1017 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
1018
1019 #ifdef HAVE_AS_TLS
1020 #undef TARGET_HAVE_TLS
1021 #define TARGET_HAVE_TLS true
1022 #endif
1023 #undef TARGET_CANNOT_FORCE_CONST_MEM
1024 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
1025
1026 #undef TARGET_DELEGITIMIZE_ADDRESS
1027 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
1028
1029 #undef TARGET_MS_BITFIELD_LAYOUT_P
1030 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
1031
1032 #undef TARGET_ASM_OUTPUT_MI_THUNK
1033 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
1034 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1035 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
1036
1037 #undef TARGET_ASM_FILE_START
1038 #define TARGET_ASM_FILE_START x86_file_start
1039
1040 #undef TARGET_RTX_COSTS
1041 #define TARGET_RTX_COSTS ix86_rtx_costs
1042 #undef TARGET_ADDRESS_COST
1043 #define TARGET_ADDRESS_COST ix86_address_cost
1044
1045 #undef TARGET_FIXED_CONDITION_CODE_REGS
1046 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
1047 #undef TARGET_CC_MODES_COMPATIBLE
1048 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
1049
1050 #undef TARGET_MACHINE_DEPENDENT_REORG
1051 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
1052
1053 #undef TARGET_BUILD_BUILTIN_VA_LIST
1054 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
1055
1056 #undef TARGET_MD_ASM_CLOBBERS
1057 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
1058
1059 #undef TARGET_PROMOTE_PROTOTYPES
1060 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1061 #undef TARGET_STRUCT_VALUE_RTX
1062 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
1063 #undef TARGET_SETUP_INCOMING_VARARGS
1064 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
1065 #undef TARGET_MUST_PASS_IN_STACK
1066 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
1067 #undef TARGET_PASS_BY_REFERENCE
1068 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
1069
1070 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1071 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
1072
1073 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1074 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
1075
1076 #ifdef SUBTARGET_INSERT_ATTRIBUTES
1077 #undef TARGET_INSERT_ATTRIBUTES
1078 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
1079 #endif
1080
1081 struct gcc_target targetm = TARGET_INITIALIZER;
1082
1083 \f
1084 /* The svr4 ABI for the i386 says that records and unions are returned
1085    in memory.  */
1086 #ifndef DEFAULT_PCC_STRUCT_RETURN
1087 #define DEFAULT_PCC_STRUCT_RETURN 1
1088 #endif
1089
1090 /* Sometimes certain combinations of command options do not make
1091    sense on a particular target machine.  You can define a macro
1092    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1093    defined, is executed once just after all the command options have
1094    been parsed.
1095
1096    Don't use this macro to turn on various extra optimizations for
1097    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1098
1099 void
1100 override_options (void)
1101 {
1102   int i;
1103   int ix86_tune_defaulted = 0;
1104
1105   /* Comes from final.c -- no real reason to change it.  */
1106 #define MAX_CODE_ALIGN 16
1107
1108   static struct ptt
1109     {
1110       const struct processor_costs *cost;       /* Processor costs */
1111       const int target_enable;                  /* Target flags to enable.  */
1112       const int target_disable;                 /* Target flags to disable.  */
1113       const int align_loop;                     /* Default alignments.  */
1114       const int align_loop_max_skip;
1115       const int align_jump;
1116       const int align_jump_max_skip;
1117       const int align_func;
1118     }
1119   const processor_target_table[PROCESSOR_max] =
1120     {
1121       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1122       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1123       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1124       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1125       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1126       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1127       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1128       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1129       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0}
1130     };
1131
1132   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1133   static struct pta
1134     {
1135       const char *const name;           /* processor name or nickname.  */
1136       const enum processor_type processor;
1137       const enum pta_flags
1138         {
1139           PTA_SSE = 1,
1140           PTA_SSE2 = 2,
1141           PTA_SSE3 = 4,
1142           PTA_MMX = 8,
1143           PTA_PREFETCH_SSE = 16,
1144           PTA_3DNOW = 32,
1145           PTA_3DNOW_A = 64,
1146           PTA_64BIT = 128
1147         } flags;
1148     }
1149   const processor_alias_table[] =
1150     {
1151       {"i386", PROCESSOR_I386, 0},
1152       {"i486", PROCESSOR_I486, 0},
1153       {"i586", PROCESSOR_PENTIUM, 0},
1154       {"pentium", PROCESSOR_PENTIUM, 0},
1155       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1156       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1157       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1158       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1159       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1160       {"i686", PROCESSOR_PENTIUMPRO, 0},
1161       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1162       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1163       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1164       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1165       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1166       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1167                                        | PTA_MMX | PTA_PREFETCH_SSE},
1168       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1169                                         | PTA_MMX | PTA_PREFETCH_SSE},
1170       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1171                                         | PTA_MMX | PTA_PREFETCH_SSE},
1172       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1173                                         | PTA_MMX | PTA_PREFETCH_SSE},
1174       {"k6", PROCESSOR_K6, PTA_MMX},
1175       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1176       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1177       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1178                                    | PTA_3DNOW_A},
1179       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1180                                          | PTA_3DNOW | PTA_3DNOW_A},
1181       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1182                                     | PTA_3DNOW_A | PTA_SSE},
1183       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1184                                       | PTA_3DNOW_A | PTA_SSE},
1185       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1186                                       | PTA_3DNOW_A | PTA_SSE},
1187       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1188                                | PTA_SSE | PTA_SSE2 },
1189       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1190                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1191       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1192                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1193       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1194                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1195       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1196                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1197     };
1198
1199   int const pta_size = ARRAY_SIZE (processor_alias_table);
1200
1201   /* Set the default values for switches whose default depends on TARGET_64BIT
1202      in case they weren't overwritten by command line options.  */
1203   if (TARGET_64BIT)
1204     {
1205       if (flag_omit_frame_pointer == 2)
1206         flag_omit_frame_pointer = 1;
1207       if (flag_asynchronous_unwind_tables == 2)
1208         flag_asynchronous_unwind_tables = 1;
1209       if (flag_pcc_struct_return == 2)
1210         flag_pcc_struct_return = 0;
1211     }
1212   else
1213     {
1214       if (flag_omit_frame_pointer == 2)
1215         flag_omit_frame_pointer = 0;
1216       if (flag_asynchronous_unwind_tables == 2)
1217         flag_asynchronous_unwind_tables = 0;
1218       if (flag_pcc_struct_return == 2)
1219         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1220     }
1221
1222 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1223   SUBTARGET_OVERRIDE_OPTIONS;
1224 #endif
1225
1226   if (!ix86_tune_string && ix86_arch_string)
1227     ix86_tune_string = ix86_arch_string;
1228   if (!ix86_tune_string)
1229     {
1230       ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1231       ix86_tune_defaulted = 1;
1232     }
1233   if (!ix86_arch_string)
1234     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1235
1236   if (ix86_cmodel_string != 0)
1237     {
1238       if (!strcmp (ix86_cmodel_string, "small"))
1239         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1240       else if (flag_pic)
1241         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1242       else if (!strcmp (ix86_cmodel_string, "32"))
1243         ix86_cmodel = CM_32;
1244       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1245         ix86_cmodel = CM_KERNEL;
1246       else if (!strcmp (ix86_cmodel_string, "medium") && !flag_pic)
1247         ix86_cmodel = CM_MEDIUM;
1248       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1249         ix86_cmodel = CM_LARGE;
1250       else
1251         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1252     }
1253   else
1254     {
1255       ix86_cmodel = CM_32;
1256       if (TARGET_64BIT)
1257         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1258     }
1259   if (ix86_asm_string != 0)
1260     {
1261       if (!strcmp (ix86_asm_string, "intel"))
1262         ix86_asm_dialect = ASM_INTEL;
1263       else if (!strcmp (ix86_asm_string, "att"))
1264         ix86_asm_dialect = ASM_ATT;
1265       else
1266         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1267     }
1268   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1269     error ("code model `%s' not supported in the %s bit mode",
1270            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1271   if (ix86_cmodel == CM_LARGE)
1272     sorry ("code model `large' not supported yet");
1273   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1274     sorry ("%i-bit mode not compiled in",
1275            (target_flags & MASK_64BIT) ? 64 : 32);
1276
1277   for (i = 0; i < pta_size; i++)
1278     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1279       {
1280         ix86_arch = processor_alias_table[i].processor;
1281         /* Default cpu tuning to the architecture.  */
1282         ix86_tune = ix86_arch;
1283         if (processor_alias_table[i].flags & PTA_MMX
1284             && !(target_flags_explicit & MASK_MMX))
1285           target_flags |= MASK_MMX;
1286         if (processor_alias_table[i].flags & PTA_3DNOW
1287             && !(target_flags_explicit & MASK_3DNOW))
1288           target_flags |= MASK_3DNOW;
1289         if (processor_alias_table[i].flags & PTA_3DNOW_A
1290             && !(target_flags_explicit & MASK_3DNOW_A))
1291           target_flags |= MASK_3DNOW_A;
1292         if (processor_alias_table[i].flags & PTA_SSE
1293             && !(target_flags_explicit & MASK_SSE))
1294           target_flags |= MASK_SSE;
1295         if (processor_alias_table[i].flags & PTA_SSE2
1296             && !(target_flags_explicit & MASK_SSE2))
1297           target_flags |= MASK_SSE2;
1298         if (processor_alias_table[i].flags & PTA_SSE3
1299             && !(target_flags_explicit & MASK_SSE3))
1300           target_flags |= MASK_SSE3;
1301         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1302           x86_prefetch_sse = true;
1303         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1304           {
1305             if (ix86_tune_defaulted)
1306               {
1307                 ix86_tune_string = "x86-64";
1308                 for (i = 0; i < pta_size; i++)
1309                   if (! strcmp (ix86_tune_string,
1310                                 processor_alias_table[i].name))
1311                     break;
1312                 ix86_tune = processor_alias_table[i].processor;
1313               }
1314             else
1315               error ("CPU you selected does not support x86-64 "
1316                      "instruction set");
1317           }
1318         break;
1319       }
1320
1321   if (i == pta_size)
1322     error ("bad value (%s) for -march= switch", ix86_arch_string);
1323
1324   for (i = 0; i < pta_size; i++)
1325     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1326       {
1327         ix86_tune = processor_alias_table[i].processor;
1328         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1329           error ("CPU you selected does not support x86-64 instruction set");
1330
1331         /* Intel CPUs have always interpreted SSE prefetch instructions as
1332            NOPs; so, we can enable SSE prefetch instructions even when
1333            -mtune (rather than -march) points us to a processor that has them.
1334            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1335            higher processors.  */
1336         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1337           x86_prefetch_sse = true;
1338         break;
1339       }
1340   if (i == pta_size)
1341     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1342
1343   if (optimize_size)
1344     ix86_cost = &size_cost;
1345   else
1346     ix86_cost = processor_target_table[ix86_tune].cost;
1347   target_flags |= processor_target_table[ix86_tune].target_enable;
1348   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1349
1350   /* Arrange to set up i386_stack_locals for all functions.  */
1351   init_machine_status = ix86_init_machine_status;
1352
1353   /* Validate -mregparm= value.  */
1354   if (ix86_regparm_string)
1355     {
1356       i = atoi (ix86_regparm_string);
1357       if (i < 0 || i > REGPARM_MAX)
1358         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1359       else
1360         ix86_regparm = i;
1361     }
1362   else
1363    if (TARGET_64BIT)
1364      ix86_regparm = REGPARM_MAX;
1365
1366   /* If the user has provided any of the -malign-* options,
1367      warn and use that value only if -falign-* is not set.
1368      Remove this code in GCC 3.2 or later.  */
1369   if (ix86_align_loops_string)
1370     {
1371       warning ("-malign-loops is obsolete, use -falign-loops");
1372       if (align_loops == 0)
1373         {
1374           i = atoi (ix86_align_loops_string);
1375           if (i < 0 || i > MAX_CODE_ALIGN)
1376             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1377           else
1378             align_loops = 1 << i;
1379         }
1380     }
1381
1382   if (ix86_align_jumps_string)
1383     {
1384       warning ("-malign-jumps is obsolete, use -falign-jumps");
1385       if (align_jumps == 0)
1386         {
1387           i = atoi (ix86_align_jumps_string);
1388           if (i < 0 || i > MAX_CODE_ALIGN)
1389             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1390           else
1391             align_jumps = 1 << i;
1392         }
1393     }
1394
1395   if (ix86_align_funcs_string)
1396     {
1397       warning ("-malign-functions is obsolete, use -falign-functions");
1398       if (align_functions == 0)
1399         {
1400           i = atoi (ix86_align_funcs_string);
1401           if (i < 0 || i > MAX_CODE_ALIGN)
1402             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1403           else
1404             align_functions = 1 << i;
1405         }
1406     }
1407
1408   /* Default align_* from the processor table.  */
1409   if (align_loops == 0)
1410     {
1411       align_loops = processor_target_table[ix86_tune].align_loop;
1412       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
1413     }
1414   if (align_jumps == 0)
1415     {
1416       align_jumps = processor_target_table[ix86_tune].align_jump;
1417       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
1418     }
1419   if (align_functions == 0)
1420     {
1421       align_functions = processor_target_table[ix86_tune].align_func;
1422     }
1423
1424   /* Validate -mpreferred-stack-boundary= value, or provide default.
1425      The default of 128 bits is for Pentium III's SSE __m128, but we
1426      don't want additional code to keep the stack aligned when
1427      optimizing for code size.  */
1428   ix86_preferred_stack_boundary = (optimize_size
1429                                    ? TARGET_64BIT ? 128 : 32
1430                                    : 128);
1431   if (ix86_preferred_stack_boundary_string)
1432     {
1433       i = atoi (ix86_preferred_stack_boundary_string);
1434       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1435         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1436                TARGET_64BIT ? 4 : 2);
1437       else
1438         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1439     }
1440
1441   /* Validate -mbranch-cost= value, or provide default.  */
1442   ix86_branch_cost = processor_target_table[ix86_tune].cost->branch_cost;
1443   if (ix86_branch_cost_string)
1444     {
1445       i = atoi (ix86_branch_cost_string);
1446       if (i < 0 || i > 5)
1447         error ("-mbranch-cost=%d is not between 0 and 5", i);
1448       else
1449         ix86_branch_cost = i;
1450     }
1451
1452   if (ix86_tls_dialect_string)
1453     {
1454       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1455         ix86_tls_dialect = TLS_DIALECT_GNU;
1456       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1457         ix86_tls_dialect = TLS_DIALECT_SUN;
1458       else
1459         error ("bad value (%s) for -mtls-dialect= switch",
1460                ix86_tls_dialect_string);
1461     }
1462
1463   /* Keep nonleaf frame pointers.  */
1464   if (TARGET_OMIT_LEAF_FRAME_POINTER)
1465     flag_omit_frame_pointer = 1;
1466
1467   /* If we're doing fast math, we don't care about comparison order
1468      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1469   if (flag_unsafe_math_optimizations)
1470     target_flags &= ~MASK_IEEE_FP;
1471
1472   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1473      since the insns won't need emulation.  */
1474   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1475     target_flags &= ~MASK_NO_FANCY_MATH_387;
1476
1477   /* Turn on SSE2 builtins for -msse3.  */
1478   if (TARGET_SSE3)
1479     target_flags |= MASK_SSE2;
1480
1481   /* Turn on SSE builtins for -msse2.  */
1482   if (TARGET_SSE2)
1483     target_flags |= MASK_SSE;
1484
1485   if (TARGET_64BIT)
1486     {
1487       if (TARGET_ALIGN_DOUBLE)
1488         error ("-malign-double makes no sense in the 64bit mode");
1489       if (TARGET_RTD)
1490         error ("-mrtd calling convention not supported in the 64bit mode");
1491       /* Enable by default the SSE and MMX builtins.  */
1492       target_flags |= (MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE);
1493       ix86_fpmath = FPMATH_SSE;
1494      }
1495   else
1496     {
1497       ix86_fpmath = FPMATH_387;
1498       /* i386 ABI does not specify red zone.  It still makes sense to use it
1499          when programmer takes care to stack from being destroyed.  */
1500       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
1501         target_flags |= MASK_NO_RED_ZONE;
1502     }
1503
1504   if (ix86_fpmath_string != 0)
1505     {
1506       if (! strcmp (ix86_fpmath_string, "387"))
1507         ix86_fpmath = FPMATH_387;
1508       else if (! strcmp (ix86_fpmath_string, "sse"))
1509         {
1510           if (!TARGET_SSE)
1511             {
1512               warning ("SSE instruction set disabled, using 387 arithmetics");
1513               ix86_fpmath = FPMATH_387;
1514             }
1515           else
1516             ix86_fpmath = FPMATH_SSE;
1517         }
1518       else if (! strcmp (ix86_fpmath_string, "387,sse")
1519                || ! strcmp (ix86_fpmath_string, "sse,387"))
1520         {
1521           if (!TARGET_SSE)
1522             {
1523               warning ("SSE instruction set disabled, using 387 arithmetics");
1524               ix86_fpmath = FPMATH_387;
1525             }
1526           else if (!TARGET_80387)
1527             {
1528               warning ("387 instruction set disabled, using SSE arithmetics");
1529               ix86_fpmath = FPMATH_SSE;
1530             }
1531           else
1532             ix86_fpmath = FPMATH_SSE | FPMATH_387;
1533         }
1534       else
1535         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
1536     }
1537
1538   /* It makes no sense to ask for just SSE builtins, so MMX is also turned
1539      on by -msse.  */
1540   if (TARGET_SSE)
1541     {
1542       target_flags |= MASK_MMX;
1543       x86_prefetch_sse = true;
1544     }
1545
1546   /* If it has 3DNow! it also has MMX so MMX is also turned on by -m3dnow */
1547   if (TARGET_3DNOW)
1548     {
1549       target_flags |= MASK_MMX;
1550       /* If we are targeting the Athlon architecture, enable the 3Dnow/MMX
1551          extensions it adds.  */
1552       if (x86_3dnow_a & (1 << ix86_arch))
1553         target_flags |= MASK_3DNOW_A;
1554     }
1555   if ((x86_accumulate_outgoing_args & TUNEMASK)
1556       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1557       && !optimize_size)
1558     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1559
1560   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1561   {
1562     char *p;
1563     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1564     p = strchr (internal_label_prefix, 'X');
1565     internal_label_prefix_len = p - internal_label_prefix;
1566     *p = '\0';
1567   }
1568 }
1569 \f
1570 void
1571 optimization_options (int level, int size ATTRIBUTE_UNUSED)
1572 {
1573   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
1574      make the problem with not enough registers even worse.  */
1575 #ifdef INSN_SCHEDULING
1576   if (level > 1)
1577     flag_schedule_insns = 0;
1578 #endif
1579
1580   /* The default values of these switches depend on the TARGET_64BIT
1581      that is not known at this moment.  Mark these values with 2 and
1582      let user the to override these.  In case there is no command line option
1583      specifying them, we will set the defaults in override_options.  */
1584   if (optimize >= 1)
1585     flag_omit_frame_pointer = 2;
1586   flag_pcc_struct_return = 2;
1587   flag_asynchronous_unwind_tables = 2;
1588 }
1589 \f
1590 /* Table of valid machine attributes.  */
1591 const struct attribute_spec ix86_attribute_table[] =
1592 {
1593   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1594   /* Stdcall attribute says callee is responsible for popping arguments
1595      if they are not variable.  */
1596   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1597   /* Fastcall attribute says callee is responsible for popping arguments
1598      if they are not variable.  */
1599   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1600   /* Cdecl attribute says the callee is a normal C declaration */
1601   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1602   /* Regparm attribute specifies how many integer arguments are to be
1603      passed in registers.  */
1604   { "regparm",   1, 1, false, true,  true,  ix86_handle_regparm_attribute },
1605 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1606   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
1607   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
1608   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
1609 #endif
1610   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1611   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1612 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1613   SUBTARGET_ATTRIBUTE_TABLE,
1614 #endif
1615   { NULL,        0, 0, false, false, false, NULL }
1616 };
1617
1618 /* Decide whether we can make a sibling call to a function.  DECL is the
1619    declaration of the function being targeted by the call and EXP is the
1620    CALL_EXPR representing the call.  */
1621
1622 static bool
1623 ix86_function_ok_for_sibcall (tree decl, tree exp)
1624 {
1625   /* If we are generating position-independent code, we cannot sibcall
1626      optimize any indirect call, or a direct call to a global function,
1627      as the PLT requires %ebx be live.  */
1628   if (!TARGET_64BIT && flag_pic && (!decl || TREE_PUBLIC (decl)))
1629     return false;
1630
1631   /* If we are returning floats on the 80387 register stack, we cannot
1632      make a sibcall from a function that doesn't return a float to a
1633      function that does or, conversely, from a function that does return
1634      a float to a function that doesn't; the necessary stack adjustment
1635      would not be executed.  */
1636   if (STACK_REG_P (ix86_function_value (TREE_TYPE (exp)))
1637       != STACK_REG_P (ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)))))
1638     return false;
1639
1640   /* If this call is indirect, we'll need to be able to use a call-clobbered
1641      register for the address of the target function.  Make sure that all
1642      such registers are not used for passing parameters.  */
1643   if (!decl && !TARGET_64BIT)
1644     {
1645       tree type;
1646
1647       /* We're looking at the CALL_EXPR, we need the type of the function.  */
1648       type = TREE_OPERAND (exp, 0);             /* pointer expression */
1649       type = TREE_TYPE (type);                  /* pointer type */
1650       type = TREE_TYPE (type);                  /* function type */
1651
1652       if (ix86_function_regparm (type, NULL) >= 3)
1653         {
1654           /* ??? Need to count the actual number of registers to be used,
1655              not the possible number of registers.  Fix later.  */
1656           return false;
1657         }
1658     }
1659
1660   /* Otherwise okay.  That also includes certain types of indirect calls.  */
1661   return true;
1662 }
1663
1664 /* Handle a "cdecl", "stdcall", or "fastcall" attribute;
1665    arguments as in struct attribute_spec.handler.  */
1666 static tree
1667 ix86_handle_cdecl_attribute (tree *node, tree name,
1668                              tree args ATTRIBUTE_UNUSED,
1669                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1670 {
1671   if (TREE_CODE (*node) != FUNCTION_TYPE
1672       && TREE_CODE (*node) != METHOD_TYPE
1673       && TREE_CODE (*node) != FIELD_DECL
1674       && TREE_CODE (*node) != TYPE_DECL)
1675     {
1676       warning ("`%s' attribute only applies to functions",
1677                IDENTIFIER_POINTER (name));
1678       *no_add_attrs = true;
1679     }
1680   else
1681     {
1682       if (is_attribute_p ("fastcall", name))
1683         {
1684           if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
1685             {
1686               error ("fastcall and stdcall attributes are not compatible");
1687             }
1688            else if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
1689             {
1690               error ("fastcall and regparm attributes are not compatible");
1691             }
1692         }
1693       else if (is_attribute_p ("stdcall", name))
1694         {
1695           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1696             {
1697               error ("fastcall and stdcall attributes are not compatible");
1698             }
1699         }
1700     }
1701
1702   if (TARGET_64BIT)
1703     {
1704       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1705       *no_add_attrs = true;
1706     }
1707
1708   return NULL_TREE;
1709 }
1710
1711 /* Handle a "regparm" attribute;
1712    arguments as in struct attribute_spec.handler.  */
1713 static tree
1714 ix86_handle_regparm_attribute (tree *node, tree name, tree args,
1715                                int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1716 {
1717   if (TREE_CODE (*node) != FUNCTION_TYPE
1718       && TREE_CODE (*node) != METHOD_TYPE
1719       && TREE_CODE (*node) != FIELD_DECL
1720       && TREE_CODE (*node) != TYPE_DECL)
1721     {
1722       warning ("`%s' attribute only applies to functions",
1723                IDENTIFIER_POINTER (name));
1724       *no_add_attrs = true;
1725     }
1726   else
1727     {
1728       tree cst;
1729
1730       cst = TREE_VALUE (args);
1731       if (TREE_CODE (cst) != INTEGER_CST)
1732         {
1733           warning ("`%s' attribute requires an integer constant argument",
1734                    IDENTIFIER_POINTER (name));
1735           *no_add_attrs = true;
1736         }
1737       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
1738         {
1739           warning ("argument to `%s' attribute larger than %d",
1740                    IDENTIFIER_POINTER (name), REGPARM_MAX);
1741           *no_add_attrs = true;
1742         }
1743
1744       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1745         {
1746           error ("fastcall and regparm attributes are not compatible");
1747         }
1748     }
1749
1750   return NULL_TREE;
1751 }
1752
1753 /* Return 0 if the attributes for two types are incompatible, 1 if they
1754    are compatible, and 2 if they are nearly compatible (which causes a
1755    warning to be generated).  */
1756
1757 static int
1758 ix86_comp_type_attributes (tree type1, tree type2)
1759 {
1760   /* Check for mismatch of non-default calling convention.  */
1761   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
1762
1763   if (TREE_CODE (type1) != FUNCTION_TYPE)
1764     return 1;
1765
1766   /*  Check for mismatched fastcall types */
1767   if (!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
1768       != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
1769     return 0;
1770
1771   /* Check for mismatched return types (cdecl vs stdcall).  */
1772   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
1773       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
1774     return 0;
1775   if (ix86_function_regparm (type1, NULL)
1776       != ix86_function_regparm (type2, NULL))
1777     return 0;
1778   return 1;
1779 }
1780 \f
1781 /* Return the regparm value for a fuctio with the indicated TYPE and DECL.
1782    DECL may be NULL when calling function indirectly
1783    or considering a libcall.  */
1784
1785 static int
1786 ix86_function_regparm (tree type, tree decl)
1787 {
1788   tree attr;
1789   int regparm = ix86_regparm;
1790   bool user_convention = false;
1791
1792   if (!TARGET_64BIT)
1793     {
1794       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
1795       if (attr)
1796         {
1797           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
1798           user_convention = true;
1799         }
1800
1801       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
1802         {
1803           regparm = 2;
1804           user_convention = true;
1805         }
1806
1807       /* Use register calling convention for local functions when possible.  */
1808       if (!TARGET_64BIT && !user_convention && decl
1809           && flag_unit_at_a_time && !profile_flag)
1810         {
1811           struct cgraph_local_info *i = cgraph_local_info (decl);
1812           if (i && i->local)
1813             {
1814               /* We can't use regparm(3) for nested functions as these use
1815                  static chain pointer in third argument.  */
1816               if (DECL_CONTEXT (decl) && !DECL_NO_STATIC_CHAIN (decl))
1817                 regparm = 2;
1818               else
1819                 regparm = 3;
1820             }
1821         }
1822     }
1823   return regparm;
1824 }
1825
1826 /* Return true if EAX is live at the start of the function.  Used by
1827    ix86_expand_prologue to determine if we need special help before
1828    calling allocate_stack_worker.  */
1829
1830 static bool
1831 ix86_eax_live_at_start_p (void)
1832 {
1833   /* Cheat.  Don't bother working forward from ix86_function_regparm
1834      to the function type to whether an actual argument is located in
1835      eax.  Instead just look at cfg info, which is still close enough
1836      to correct at this point.  This gives false positives for broken
1837      functions that might use uninitialized data that happens to be
1838      allocated in eax, but who cares?  */
1839   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->global_live_at_end, 0);
1840 }
1841
1842 /* Value is the number of bytes of arguments automatically
1843    popped when returning from a subroutine call.
1844    FUNDECL is the declaration node of the function (as a tree),
1845    FUNTYPE is the data type of the function (as a tree),
1846    or for a library call it is an identifier node for the subroutine name.
1847    SIZE is the number of bytes of arguments passed on the stack.
1848
1849    On the 80386, the RTD insn may be used to pop them if the number
1850      of args is fixed, but if the number is variable then the caller
1851      must pop them all.  RTD can't be used for library calls now
1852      because the library is compiled with the Unix compiler.
1853    Use of RTD is a selectable option, since it is incompatible with
1854    standard Unix calling sequences.  If the option is not selected,
1855    the caller must always pop the args.
1856
1857    The attribute stdcall is equivalent to RTD on a per module basis.  */
1858
1859 int
1860 ix86_return_pops_args (tree fundecl, tree funtype, int size)
1861 {
1862   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
1863
1864   /* Cdecl functions override -mrtd, and never pop the stack.  */
1865   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
1866
1867     /* Stdcall and fastcall functions will pop the stack if not
1868        variable args.  */
1869     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
1870         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
1871       rtd = 1;
1872
1873     if (rtd
1874         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
1875             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
1876                 == void_type_node)))
1877       return size;
1878   }
1879
1880   /* Lose any fake structure return argument if it is passed on the stack.  */
1881   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
1882       && !TARGET_64BIT
1883       && !KEEP_AGGREGATE_RETURN_POINTER)
1884     {
1885       int nregs = ix86_function_regparm (funtype, fundecl);
1886
1887       if (!nregs)
1888         return GET_MODE_SIZE (Pmode);
1889     }
1890
1891   return 0;
1892 }
1893 \f
1894 /* Argument support functions.  */
1895
1896 /* Return true when register may be used to pass function parameters.  */
1897 bool
1898 ix86_function_arg_regno_p (int regno)
1899 {
1900   int i;
1901   if (!TARGET_64BIT)
1902     return (regno < REGPARM_MAX
1903             || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
1904   if (SSE_REGNO_P (regno) && TARGET_SSE)
1905     return true;
1906   /* RAX is used as hidden argument to va_arg functions.  */
1907   if (!regno)
1908     return true;
1909   for (i = 0; i < REGPARM_MAX; i++)
1910     if (regno == x86_64_int_parameter_registers[i])
1911       return true;
1912   return false;
1913 }
1914
1915 /* Return if we do not know how to pass TYPE solely in registers.  */
1916
1917 static bool
1918 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
1919 {
1920   if (must_pass_in_stack_var_size_or_pad (mode, type))
1921     return true;
1922   return (!TARGET_64BIT && type && mode == TImode);
1923 }
1924
1925 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1926    for a call to a function whose data type is FNTYPE.
1927    For a library call, FNTYPE is 0.  */
1928
1929 void
1930 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
1931                       tree fntype,      /* tree ptr for function decl */
1932                       rtx libname,      /* SYMBOL_REF of library name or 0 */
1933                       tree fndecl)
1934 {
1935   static CUMULATIVE_ARGS zero_cum;
1936   tree param, next_param;
1937
1938   if (TARGET_DEBUG_ARG)
1939     {
1940       fprintf (stderr, "\ninit_cumulative_args (");
1941       if (fntype)
1942         fprintf (stderr, "fntype code = %s, ret code = %s",
1943                  tree_code_name[(int) TREE_CODE (fntype)],
1944                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
1945       else
1946         fprintf (stderr, "no fntype");
1947
1948       if (libname)
1949         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
1950     }
1951
1952   *cum = zero_cum;
1953
1954   /* Set up the number of registers to use for passing arguments.  */
1955   if (fntype)
1956     cum->nregs = ix86_function_regparm (fntype, fndecl);
1957   else
1958     cum->nregs = ix86_regparm;
1959   if (TARGET_SSE)
1960     cum->sse_nregs = SSE_REGPARM_MAX;
1961   if (TARGET_MMX)
1962     cum->mmx_nregs = MMX_REGPARM_MAX;
1963   cum->warn_sse = true;
1964   cum->warn_mmx = true;
1965   cum->maybe_vaarg = false;
1966
1967   /* Use ecx and edx registers if function has fastcall attribute */
1968   if (fntype && !TARGET_64BIT)
1969     {
1970       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
1971         {
1972           cum->nregs = 2;
1973           cum->fastcall = 1;
1974         }
1975     }
1976
1977   /* Determine if this function has variable arguments.  This is
1978      indicated by the last argument being 'void_type_mode' if there
1979      are no variable arguments.  If there are variable arguments, then
1980      we won't pass anything in registers in 32-bit mode. */
1981
1982   if (cum->nregs || cum->mmx_nregs || cum->sse_nregs)
1983     {
1984       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
1985            param != 0; param = next_param)
1986         {
1987           next_param = TREE_CHAIN (param);
1988           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
1989             {
1990               if (!TARGET_64BIT)
1991                 {
1992                   cum->nregs = 0;
1993                   cum->sse_nregs = 0;
1994                   cum->mmx_nregs = 0;
1995                   cum->warn_sse = 0;
1996                   cum->warn_mmx = 0;
1997                   cum->fastcall = 0;
1998                 }
1999               cum->maybe_vaarg = true;
2000             }
2001         }
2002     }
2003   if ((!fntype && !libname)
2004       || (fntype && !TYPE_ARG_TYPES (fntype)))
2005     cum->maybe_vaarg = 1;
2006
2007   if (TARGET_DEBUG_ARG)
2008     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
2009
2010   return;
2011 }
2012
2013 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
2014    of this code is to classify each 8bytes of incoming argument by the register
2015    class and assign registers accordingly.  */
2016
2017 /* Return the union class of CLASS1 and CLASS2.
2018    See the x86-64 PS ABI for details.  */
2019
2020 static enum x86_64_reg_class
2021 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
2022 {
2023   /* Rule #1: If both classes are equal, this is the resulting class.  */
2024   if (class1 == class2)
2025     return class1;
2026
2027   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
2028      the other class.  */
2029   if (class1 == X86_64_NO_CLASS)
2030     return class2;
2031   if (class2 == X86_64_NO_CLASS)
2032     return class1;
2033
2034   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
2035   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
2036     return X86_64_MEMORY_CLASS;
2037
2038   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
2039   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
2040       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
2041     return X86_64_INTEGERSI_CLASS;
2042   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
2043       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
2044     return X86_64_INTEGER_CLASS;
2045
2046   /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used.  */
2047   if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS
2048       || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)
2049     return X86_64_MEMORY_CLASS;
2050
2051   /* Rule #6: Otherwise class SSE is used.  */
2052   return X86_64_SSE_CLASS;
2053 }
2054
2055 /* Classify the argument of type TYPE and mode MODE.
2056    CLASSES will be filled by the register class used to pass each word
2057    of the operand.  The number of words is returned.  In case the parameter
2058    should be passed in memory, 0 is returned. As a special case for zero
2059    sized containers, classes[0] will be NO_CLASS and 1 is returned.
2060
2061    BIT_OFFSET is used internally for handling records and specifies offset
2062    of the offset in bits modulo 256 to avoid overflow cases.
2063
2064    See the x86-64 PS ABI for details.
2065 */
2066
2067 static int
2068 classify_argument (enum machine_mode mode, tree type,
2069                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
2070 {
2071   HOST_WIDE_INT bytes =
2072     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2073   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2074
2075   /* Variable sized entities are always passed/returned in memory.  */
2076   if (bytes < 0)
2077     return 0;
2078
2079   if (mode != VOIDmode
2080       && targetm.calls.must_pass_in_stack (mode, type))
2081     return 0;
2082
2083   if (type && AGGREGATE_TYPE_P (type))
2084     {
2085       int i;
2086       tree field;
2087       enum x86_64_reg_class subclasses[MAX_CLASSES];
2088
2089       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
2090       if (bytes > 16)
2091         return 0;
2092
2093       for (i = 0; i < words; i++)
2094         classes[i] = X86_64_NO_CLASS;
2095
2096       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
2097          signalize memory class, so handle it as special case.  */
2098       if (!words)
2099         {
2100           classes[0] = X86_64_NO_CLASS;
2101           return 1;
2102         }
2103
2104       /* Classify each field of record and merge classes.  */
2105       if (TREE_CODE (type) == RECORD_TYPE)
2106         {
2107           /* For classes first merge in the field of the subclasses.  */
2108           if (TYPE_BINFO (type))
2109             {
2110               tree binfo, base_binfo;
2111               int i;
2112
2113               for (binfo = TYPE_BINFO (type), i = 0;
2114                    BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2115                 {
2116                    int num;
2117                    int offset = tree_low_cst (BINFO_OFFSET (base_binfo), 0) * 8;
2118                    tree type = BINFO_TYPE (base_binfo);
2119
2120                    num = classify_argument (TYPE_MODE (type),
2121                                             type, subclasses,
2122                                             (offset + bit_offset) % 256);
2123                    if (!num)
2124                      return 0;
2125                    for (i = 0; i < num; i++)
2126                      {
2127                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2128                        classes[i + pos] =
2129                          merge_classes (subclasses[i], classes[i + pos]);
2130                      }
2131                 }
2132             }
2133           /* And now merge the fields of structure.  */
2134           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2135             {
2136               if (TREE_CODE (field) == FIELD_DECL)
2137                 {
2138                   int num;
2139
2140                   /* Bitfields are always classified as integer.  Handle them
2141                      early, since later code would consider them to be
2142                      misaligned integers.  */
2143                   if (DECL_BIT_FIELD (field))
2144                     {
2145                       for (i = int_bit_position (field) / 8 / 8;
2146                            i < (int_bit_position (field)
2147                                 + tree_low_cst (DECL_SIZE (field), 0)
2148                                 + 63) / 8 / 8; i++)
2149                         classes[i] =
2150                           merge_classes (X86_64_INTEGER_CLASS,
2151                                          classes[i]);
2152                     }
2153                   else
2154                     {
2155                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2156                                                TREE_TYPE (field), subclasses,
2157                                                (int_bit_position (field)
2158                                                 + bit_offset) % 256);
2159                       if (!num)
2160                         return 0;
2161                       for (i = 0; i < num; i++)
2162                         {
2163                           int pos =
2164                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
2165                           classes[i + pos] =
2166                             merge_classes (subclasses[i], classes[i + pos]);
2167                         }
2168                     }
2169                 }
2170             }
2171         }
2172       /* Arrays are handled as small records.  */
2173       else if (TREE_CODE (type) == ARRAY_TYPE)
2174         {
2175           int num;
2176           num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
2177                                    TREE_TYPE (type), subclasses, bit_offset);
2178           if (!num)
2179             return 0;
2180
2181           /* The partial classes are now full classes.  */
2182           if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
2183             subclasses[0] = X86_64_SSE_CLASS;
2184           if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
2185             subclasses[0] = X86_64_INTEGER_CLASS;
2186
2187           for (i = 0; i < words; i++)
2188             classes[i] = subclasses[i % num];
2189         }
2190       /* Unions are similar to RECORD_TYPE but offset is always 0.  */
2191       else if (TREE_CODE (type) == UNION_TYPE
2192                || TREE_CODE (type) == QUAL_UNION_TYPE)
2193         {
2194           /* For classes first merge in the field of the subclasses.  */
2195           if (TYPE_BINFO (type))
2196             {
2197               tree binfo, base_binfo;
2198               int i;
2199
2200               for (binfo = TYPE_BINFO (type), i = 0;
2201                    BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2202                 {
2203                    int num;
2204                    int offset = tree_low_cst (BINFO_OFFSET (base_binfo), 0) * 8;
2205                    tree type = BINFO_TYPE (base_binfo);
2206
2207                    num = classify_argument (TYPE_MODE (type),
2208                                             type, subclasses,
2209                                             (offset + (bit_offset % 64)) % 256);
2210                    if (!num)
2211                      return 0;
2212                    for (i = 0; i < num; i++)
2213                      {
2214                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2215                        classes[i + pos] =
2216                          merge_classes (subclasses[i], classes[i + pos]);
2217                      }
2218                 }
2219             }
2220           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2221             {
2222               if (TREE_CODE (field) == FIELD_DECL)
2223                 {
2224                   int num;
2225                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2226                                            TREE_TYPE (field), subclasses,
2227                                            bit_offset);
2228                   if (!num)
2229                     return 0;
2230                   for (i = 0; i < num; i++)
2231                     classes[i] = merge_classes (subclasses[i], classes[i]);
2232                 }
2233             }
2234         }
2235       else if (TREE_CODE (type) == SET_TYPE)
2236         {
2237           if (bytes <= 4)
2238             {
2239               classes[0] = X86_64_INTEGERSI_CLASS;
2240               return 1;
2241             }
2242           else if (bytes <= 8)
2243             {
2244               classes[0] = X86_64_INTEGER_CLASS;
2245               return 1;
2246             }
2247           else if (bytes <= 12)
2248             {
2249               classes[0] = X86_64_INTEGER_CLASS;
2250               classes[1] = X86_64_INTEGERSI_CLASS;
2251               return 2;
2252             }
2253           else
2254             {
2255               classes[0] = X86_64_INTEGER_CLASS;
2256               classes[1] = X86_64_INTEGER_CLASS;
2257               return 2;
2258             }
2259         }
2260       else
2261         abort ();
2262
2263       /* Final merger cleanup.  */
2264       for (i = 0; i < words; i++)
2265         {
2266           /* If one class is MEMORY, everything should be passed in
2267              memory.  */
2268           if (classes[i] == X86_64_MEMORY_CLASS)
2269             return 0;
2270
2271           /* The X86_64_SSEUP_CLASS should be always preceded by
2272              X86_64_SSE_CLASS.  */
2273           if (classes[i] == X86_64_SSEUP_CLASS
2274               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
2275             classes[i] = X86_64_SSE_CLASS;
2276
2277           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
2278           if (classes[i] == X86_64_X87UP_CLASS
2279               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
2280             classes[i] = X86_64_SSE_CLASS;
2281         }
2282       return words;
2283     }
2284
2285   /* Compute alignment needed.  We align all types to natural boundaries with
2286      exception of XFmode that is aligned to 64bits.  */
2287   if (mode != VOIDmode && mode != BLKmode)
2288     {
2289       int mode_alignment = GET_MODE_BITSIZE (mode);
2290
2291       if (mode == XFmode)
2292         mode_alignment = 128;
2293       else if (mode == XCmode)
2294         mode_alignment = 256;
2295       if (COMPLEX_MODE_P (mode))
2296         mode_alignment /= 2;
2297       /* Misaligned fields are always returned in memory.  */
2298       if (bit_offset % mode_alignment)
2299         return 0;
2300     }
2301
2302   /* for V1xx modes, just use the base mode */
2303   if (VECTOR_MODE_P (mode)
2304       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
2305     mode = GET_MODE_INNER (mode);
2306
2307   /* Classification of atomic types.  */
2308   switch (mode)
2309     {
2310     case DImode:
2311     case SImode:
2312     case HImode:
2313     case QImode:
2314     case CSImode:
2315     case CHImode:
2316     case CQImode:
2317       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2318         classes[0] = X86_64_INTEGERSI_CLASS;
2319       else
2320         classes[0] = X86_64_INTEGER_CLASS;
2321       return 1;
2322     case CDImode:
2323     case TImode:
2324       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
2325       return 2;
2326     case CTImode:
2327       return 0;
2328     case SFmode:
2329       if (!(bit_offset % 64))
2330         classes[0] = X86_64_SSESF_CLASS;
2331       else
2332         classes[0] = X86_64_SSE_CLASS;
2333       return 1;
2334     case DFmode:
2335       classes[0] = X86_64_SSEDF_CLASS;
2336       return 1;
2337     case XFmode:
2338       classes[0] = X86_64_X87_CLASS;
2339       classes[1] = X86_64_X87UP_CLASS;
2340       return 2;
2341     case TFmode:
2342       classes[0] = X86_64_SSE_CLASS;
2343       classes[1] = X86_64_SSEUP_CLASS;
2344       return 2;
2345     case SCmode:
2346       classes[0] = X86_64_SSE_CLASS;
2347       return 1;
2348     case DCmode:
2349       classes[0] = X86_64_SSEDF_CLASS;
2350       classes[1] = X86_64_SSEDF_CLASS;
2351       return 2;
2352     case XCmode:
2353     case TCmode:
2354       /* These modes are larger than 16 bytes.  */
2355       return 0;
2356     case V4SFmode:
2357     case V4SImode:
2358     case V16QImode:
2359     case V8HImode:
2360     case V2DFmode:
2361     case V2DImode:
2362       classes[0] = X86_64_SSE_CLASS;
2363       classes[1] = X86_64_SSEUP_CLASS;
2364       return 2;
2365     case V2SFmode:
2366     case V2SImode:
2367     case V4HImode:
2368     case V8QImode:
2369       classes[0] = X86_64_SSE_CLASS;
2370       return 1;
2371     case BLKmode:
2372     case VOIDmode:
2373       return 0;
2374     default:
2375       if (VECTOR_MODE_P (mode))
2376         {
2377           if (bytes > 16)
2378             return 0;
2379           if (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT)
2380             {
2381               if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2382                 classes[0] = X86_64_INTEGERSI_CLASS;
2383               else
2384                 classes[0] = X86_64_INTEGER_CLASS;
2385               classes[1] = X86_64_INTEGER_CLASS;
2386               return 1 + (bytes > 8);
2387             }
2388         }
2389       abort ();
2390     }
2391 }
2392
2393 /* Examine the argument and return set number of register required in each
2394    class.  Return 0 iff parameter should be passed in memory.  */
2395 static int
2396 examine_argument (enum machine_mode mode, tree type, int in_return,
2397                   int *int_nregs, int *sse_nregs)
2398 {
2399   enum x86_64_reg_class class[MAX_CLASSES];
2400   int n = classify_argument (mode, type, class, 0);
2401
2402   *int_nregs = 0;
2403   *sse_nregs = 0;
2404   if (!n)
2405     return 0;
2406   for (n--; n >= 0; n--)
2407     switch (class[n])
2408       {
2409       case X86_64_INTEGER_CLASS:
2410       case X86_64_INTEGERSI_CLASS:
2411         (*int_nregs)++;
2412         break;
2413       case X86_64_SSE_CLASS:
2414       case X86_64_SSESF_CLASS:
2415       case X86_64_SSEDF_CLASS:
2416         (*sse_nregs)++;
2417         break;
2418       case X86_64_NO_CLASS:
2419       case X86_64_SSEUP_CLASS:
2420         break;
2421       case X86_64_X87_CLASS:
2422       case X86_64_X87UP_CLASS:
2423         if (!in_return)
2424           return 0;
2425         break;
2426       case X86_64_MEMORY_CLASS:
2427         abort ();
2428       }
2429   return 1;
2430 }
2431 /* Construct container for the argument used by GCC interface.  See
2432    FUNCTION_ARG for the detailed description.  */
2433 static rtx
2434 construct_container (enum machine_mode mode, tree type, int in_return,
2435                      int nintregs, int nsseregs, const int * intreg,
2436                      int sse_regno)
2437 {
2438   enum machine_mode tmpmode;
2439   int bytes =
2440     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2441   enum x86_64_reg_class class[MAX_CLASSES];
2442   int n;
2443   int i;
2444   int nexps = 0;
2445   int needed_sseregs, needed_intregs;
2446   rtx exp[MAX_CLASSES];
2447   rtx ret;
2448
2449   n = classify_argument (mode, type, class, 0);
2450   if (TARGET_DEBUG_ARG)
2451     {
2452       if (!n)
2453         fprintf (stderr, "Memory class\n");
2454       else
2455         {
2456           fprintf (stderr, "Classes:");
2457           for (i = 0; i < n; i++)
2458             {
2459               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
2460             }
2461            fprintf (stderr, "\n");
2462         }
2463     }
2464   if (!n)
2465     return NULL;
2466   if (!examine_argument (mode, type, in_return, &needed_intregs, &needed_sseregs))
2467     return NULL;
2468   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
2469     return NULL;
2470
2471   /* First construct simple cases.  Avoid SCmode, since we want to use
2472      single register to pass this type.  */
2473   if (n == 1 && mode != SCmode)
2474     switch (class[0])
2475       {
2476       case X86_64_INTEGER_CLASS:
2477       case X86_64_INTEGERSI_CLASS:
2478         return gen_rtx_REG (mode, intreg[0]);
2479       case X86_64_SSE_CLASS:
2480       case X86_64_SSESF_CLASS:
2481       case X86_64_SSEDF_CLASS:
2482         return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2483       case X86_64_X87_CLASS:
2484         return gen_rtx_REG (mode, FIRST_STACK_REG);
2485       case X86_64_NO_CLASS:
2486         /* Zero sized array, struct or class.  */
2487         return NULL;
2488       default:
2489         abort ();
2490       }
2491   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
2492       && mode != BLKmode)
2493     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2494   if (n == 2
2495       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
2496     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
2497   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
2498       && class[1] == X86_64_INTEGER_CLASS
2499       && (mode == CDImode || mode == TImode || mode == TFmode)
2500       && intreg[0] + 1 == intreg[1])
2501     return gen_rtx_REG (mode, intreg[0]);
2502   if (n == 4
2503       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS
2504       && class[2] == X86_64_X87_CLASS && class[3] == X86_64_X87UP_CLASS
2505       && mode != BLKmode)
2506     return gen_rtx_REG (XCmode, FIRST_STACK_REG);
2507
2508   /* Otherwise figure out the entries of the PARALLEL.  */
2509   for (i = 0; i < n; i++)
2510     {
2511       switch (class[i])
2512         {
2513           case X86_64_NO_CLASS:
2514             break;
2515           case X86_64_INTEGER_CLASS:
2516           case X86_64_INTEGERSI_CLASS:
2517             /* Merge TImodes on aligned occasions here too.  */
2518             if (i * 8 + 8 > bytes)
2519               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
2520             else if (class[i] == X86_64_INTEGERSI_CLASS)
2521               tmpmode = SImode;
2522             else
2523               tmpmode = DImode;
2524             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
2525             if (tmpmode == BLKmode)
2526               tmpmode = DImode;
2527             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2528                                                gen_rtx_REG (tmpmode, *intreg),
2529                                                GEN_INT (i*8));
2530             intreg++;
2531             break;
2532           case X86_64_SSESF_CLASS:
2533             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2534                                                gen_rtx_REG (SFmode,
2535                                                             SSE_REGNO (sse_regno)),
2536                                                GEN_INT (i*8));
2537             sse_regno++;
2538             break;
2539           case X86_64_SSEDF_CLASS:
2540             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2541                                                gen_rtx_REG (DFmode,
2542                                                             SSE_REGNO (sse_regno)),
2543                                                GEN_INT (i*8));
2544             sse_regno++;
2545             break;
2546           case X86_64_SSE_CLASS:
2547             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
2548               tmpmode = TImode;
2549             else
2550               tmpmode = DImode;
2551             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2552                                                gen_rtx_REG (tmpmode,
2553                                                             SSE_REGNO (sse_regno)),
2554                                                GEN_INT (i*8));
2555             if (tmpmode == TImode)
2556               i++;
2557             sse_regno++;
2558             break;
2559           default:
2560             abort ();
2561         }
2562     }
2563   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
2564   for (i = 0; i < nexps; i++)
2565     XVECEXP (ret, 0, i) = exp [i];
2566   return ret;
2567 }
2568
2569 /* Update the data in CUM to advance over an argument
2570    of mode MODE and data type TYPE.
2571    (TYPE is null for libcalls where that information may not be available.)  */
2572
2573 void
2574 function_arg_advance (CUMULATIVE_ARGS *cum,     /* current arg information */
2575                       enum machine_mode mode,   /* current arg mode */
2576                       tree type,        /* type of the argument or 0 if lib support */
2577                       int named)        /* whether or not the argument was named */
2578 {
2579   int bytes =
2580     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2581   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2582
2583   if (TARGET_DEBUG_ARG)
2584     fprintf (stderr,
2585              "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, mode=%s, named=%d)\n\n",
2586              words, cum->words, cum->nregs, cum->sse_nregs, GET_MODE_NAME (mode), named);
2587   if (TARGET_64BIT)
2588     {
2589       int int_nregs, sse_nregs;
2590       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
2591         cum->words += words;
2592       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
2593         {
2594           cum->nregs -= int_nregs;
2595           cum->sse_nregs -= sse_nregs;
2596           cum->regno += int_nregs;
2597           cum->sse_regno += sse_nregs;
2598         }
2599       else
2600         cum->words += words;
2601     }
2602   else
2603     {
2604       if (TARGET_SSE && SSE_REG_MODE_P (mode)
2605           && (!type || !AGGREGATE_TYPE_P (type)))
2606         {
2607           cum->sse_words += words;
2608           cum->sse_nregs -= 1;
2609           cum->sse_regno += 1;
2610           if (cum->sse_nregs <= 0)
2611             {
2612               cum->sse_nregs = 0;
2613               cum->sse_regno = 0;
2614             }
2615         }
2616       else if (TARGET_MMX && MMX_REG_MODE_P (mode)
2617                && (!type || !AGGREGATE_TYPE_P (type)))
2618         {
2619           cum->mmx_words += words;
2620           cum->mmx_nregs -= 1;
2621           cum->mmx_regno += 1;
2622           if (cum->mmx_nregs <= 0)
2623             {
2624               cum->mmx_nregs = 0;
2625               cum->mmx_regno = 0;
2626             }
2627         }
2628       else
2629         {
2630           cum->words += words;
2631           cum->nregs -= words;
2632           cum->regno += words;
2633
2634           if (cum->nregs <= 0)
2635             {
2636               cum->nregs = 0;
2637               cum->regno = 0;
2638             }
2639         }
2640     }
2641   return;
2642 }
2643
2644 /* Define where to put the arguments to a function.
2645    Value is zero to push the argument on the stack,
2646    or a hard register in which to store the argument.
2647
2648    MODE is the argument's machine mode.
2649    TYPE is the data type of the argument (as a tree).
2650     This is null for libcalls where that information may
2651     not be available.
2652    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2653     the preceding args and about the function being called.
2654    NAMED is nonzero if this argument is a named parameter
2655     (otherwise it is an extra parameter matching an ellipsis).  */
2656
2657 rtx
2658 function_arg (CUMULATIVE_ARGS *cum,     /* current arg information */
2659               enum machine_mode mode,   /* current arg mode */
2660               tree type,        /* type of the argument or 0 if lib support */
2661               int named)        /* != 0 for normal args, == 0 for ...  args */
2662 {
2663   rtx ret   = NULL_RTX;
2664   int bytes =
2665     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2666   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2667   static bool warnedsse, warnedmmx;
2668
2669   /* To simplify the code below, represent vector types with a vector mode
2670      even if MMX/SSE are not active.  */
2671   if (type
2672       && TREE_CODE (type) == VECTOR_TYPE
2673       && (bytes == 8 || bytes == 16)
2674       && GET_MODE_CLASS (TYPE_MODE (type)) != MODE_VECTOR_INT
2675       && GET_MODE_CLASS (TYPE_MODE (type)) != MODE_VECTOR_FLOAT)
2676     {
2677       enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
2678       enum machine_mode newmode
2679         = TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
2680           ? MIN_MODE_VECTOR_FLOAT : MIN_MODE_VECTOR_INT;
2681
2682       /* Get the mode which has this inner mode and number of units.  */
2683       for (; newmode != VOIDmode; newmode = GET_MODE_WIDER_MODE (newmode))
2684         if (GET_MODE_NUNITS (newmode) == TYPE_VECTOR_SUBPARTS (type)
2685             && GET_MODE_INNER (newmode) == innermode)
2686           {
2687             mode = newmode;
2688             break;
2689           }
2690     }
2691
2692   /* Handle a hidden AL argument containing number of registers for varargs
2693      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
2694      any AL settings.  */
2695   if (mode == VOIDmode)
2696     {
2697       if (TARGET_64BIT)
2698         return GEN_INT (cum->maybe_vaarg
2699                         ? (cum->sse_nregs < 0
2700                            ? SSE_REGPARM_MAX
2701                            : cum->sse_regno)
2702                         : -1);
2703       else
2704         return constm1_rtx;
2705     }
2706   if (TARGET_64BIT)
2707     ret = construct_container (mode, type, 0, cum->nregs, cum->sse_nregs,
2708                                &x86_64_int_parameter_registers [cum->regno],
2709                                cum->sse_regno);
2710   else
2711     switch (mode)
2712       {
2713         /* For now, pass fp/complex values on the stack.  */
2714       default:
2715         break;
2716
2717       case BLKmode:
2718         if (bytes < 0)
2719           break;
2720         /* FALLTHRU */
2721       case DImode:
2722       case SImode:
2723       case HImode:
2724       case QImode:
2725         if (words <= cum->nregs)
2726           {
2727             int regno = cum->regno;
2728
2729             /* Fastcall allocates the first two DWORD (SImode) or
2730                smaller arguments to ECX and EDX.  */
2731             if (cum->fastcall)
2732               {
2733                 if (mode == BLKmode || mode == DImode)
2734                   break;
2735
2736                 /* ECX not EAX is the first allocated register.  */
2737                 if (regno == 0)
2738                   regno = 2;
2739               }
2740             ret = gen_rtx_REG (mode, regno);
2741           }
2742         break;
2743       case TImode:
2744       case V16QImode:
2745       case V8HImode:
2746       case V4SImode:
2747       case V2DImode:
2748       case V4SFmode:
2749       case V2DFmode:
2750         if (!type || !AGGREGATE_TYPE_P (type))
2751           {
2752             if (!TARGET_SSE && !warnedsse && cum->warn_sse)
2753               {
2754                 warnedsse = true;
2755                 warning ("SSE vector argument without SSE enabled "
2756                          "changes the ABI");
2757               }
2758             if (cum->sse_nregs)
2759               ret = gen_rtx_REG (mode, cum->sse_regno + FIRST_SSE_REG);
2760           }
2761         break;
2762       case V8QImode:
2763       case V4HImode:
2764       case V2SImode:
2765       case V2SFmode:
2766         if (!type || !AGGREGATE_TYPE_P (type))
2767           {
2768             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
2769               {
2770                 warnedmmx = true;
2771                 warning ("MMX vector argument without MMX enabled "
2772                          "changes the ABI");
2773               }
2774             if (cum->mmx_nregs)
2775               ret = gen_rtx_REG (mode, cum->mmx_regno + FIRST_MMX_REG);
2776           }
2777         break;
2778       }
2779
2780   if (TARGET_DEBUG_ARG)
2781     {
2782       fprintf (stderr,
2783                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
2784                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
2785
2786       if (ret)
2787         print_simple_rtl (stderr, ret);
2788       else
2789         fprintf (stderr, ", stack");
2790
2791       fprintf (stderr, " )\n");
2792     }
2793
2794   return ret;
2795 }
2796
2797 /* A C expression that indicates when an argument must be passed by
2798    reference.  If nonzero for an argument, a copy of that argument is
2799    made in memory and a pointer to the argument is passed instead of
2800    the argument itself.  The pointer is passed in whatever way is
2801    appropriate for passing a pointer to that type.  */
2802
2803 static bool
2804 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2805                         enum machine_mode mode ATTRIBUTE_UNUSED,
2806                         tree type, bool named ATTRIBUTE_UNUSED)
2807 {
2808   if (!TARGET_64BIT)
2809     return 0;
2810
2811   if (type && int_size_in_bytes (type) == -1)
2812     {
2813       if (TARGET_DEBUG_ARG)
2814         fprintf (stderr, "function_arg_pass_by_reference\n");
2815       return 1;
2816     }
2817
2818   return 0;
2819 }
2820
2821 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
2822    ABI.  Only called if TARGET_SSE.  */
2823 static bool
2824 contains_128bit_aligned_vector_p (tree type)
2825 {
2826   enum machine_mode mode = TYPE_MODE (type);
2827   if (SSE_REG_MODE_P (mode)
2828       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
2829     return true;
2830   if (TYPE_ALIGN (type) < 128)
2831     return false;
2832
2833   if (AGGREGATE_TYPE_P (type))
2834     {
2835       /* Walk the aggregates recursively.  */
2836       if (TREE_CODE (type) == RECORD_TYPE
2837           || TREE_CODE (type) == UNION_TYPE
2838           || TREE_CODE (type) == QUAL_UNION_TYPE)
2839         {
2840           tree field;
2841
2842           if (TYPE_BINFO (type))
2843             {
2844               tree binfo, base_binfo;
2845               int i;
2846
2847               for (binfo = TYPE_BINFO (type), i = 0;
2848                    BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2849                 if (contains_128bit_aligned_vector_p (BINFO_TYPE (base_binfo)))
2850                   return true;
2851             }
2852           /* And now merge the fields of structure.  */
2853           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2854             {
2855               if (TREE_CODE (field) == FIELD_DECL
2856                   && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
2857                 return true;
2858             }
2859         }
2860       /* Just for use if some languages passes arrays by value.  */
2861       else if (TREE_CODE (type) == ARRAY_TYPE)
2862         {
2863           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
2864             return true;
2865         }
2866       else
2867         abort ();
2868     }
2869   return false;
2870 }
2871
2872 /* Gives the alignment boundary, in bits, of an argument with the
2873    specified mode and type.  */
2874
2875 int
2876 ix86_function_arg_boundary (enum machine_mode mode, tree type)
2877 {
2878   int align;
2879   if (type)
2880     align = TYPE_ALIGN (type);
2881   else
2882     align = GET_MODE_ALIGNMENT (mode);
2883   if (align < PARM_BOUNDARY)
2884     align = PARM_BOUNDARY;
2885   if (!TARGET_64BIT)
2886     {
2887       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
2888          make an exception for SSE modes since these require 128bit
2889          alignment.
2890
2891          The handling here differs from field_alignment.  ICC aligns MMX
2892          arguments to 4 byte boundaries, while structure fields are aligned
2893          to 8 byte boundaries.  */
2894       if (!TARGET_SSE)
2895         align = PARM_BOUNDARY;
2896       else if (!type)
2897         {
2898           if (!SSE_REG_MODE_P (mode))
2899             align = PARM_BOUNDARY;
2900         }
2901       else
2902         {
2903           if (!contains_128bit_aligned_vector_p (type))
2904             align = PARM_BOUNDARY;
2905         }
2906     }
2907   if (align > 128)
2908     align = 128;
2909   return align;
2910 }
2911
2912 /* Return true if N is a possible register number of function value.  */
2913 bool
2914 ix86_function_value_regno_p (int regno)
2915 {
2916   if (!TARGET_64BIT)
2917     {
2918       return ((regno) == 0
2919               || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
2920               || ((regno) == FIRST_SSE_REG && TARGET_SSE));
2921     }
2922   return ((regno) == 0 || (regno) == FIRST_FLOAT_REG
2923           || ((regno) == FIRST_SSE_REG && TARGET_SSE)
2924           || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387));
2925 }
2926
2927 /* Define how to find the value returned by a function.
2928    VALTYPE is the data type of the value (as a tree).
2929    If the precise function being called is known, FUNC is its FUNCTION_DECL;
2930    otherwise, FUNC is 0.  */
2931 rtx
2932 ix86_function_value (tree valtype)
2933 {
2934   if (TARGET_64BIT)
2935     {
2936       rtx ret = construct_container (TYPE_MODE (valtype), valtype, 1,
2937                                      REGPARM_MAX, SSE_REGPARM_MAX,
2938                                      x86_64_int_return_registers, 0);
2939       /* For zero sized structures, construct_container return NULL, but we need
2940          to keep rest of compiler happy by returning meaningful value.  */
2941       if (!ret)
2942         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
2943       return ret;
2944     }
2945   else
2946     return gen_rtx_REG (TYPE_MODE (valtype),
2947                         ix86_value_regno (TYPE_MODE (valtype)));
2948 }
2949
2950 /* Return false iff type is returned in memory.  */
2951 int
2952 ix86_return_in_memory (tree type)
2953 {
2954   int needed_intregs, needed_sseregs, size;
2955   enum machine_mode mode = TYPE_MODE (type);
2956
2957   if (TARGET_64BIT)
2958     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
2959
2960   if (mode == BLKmode)
2961     return 1;
2962
2963   size = int_size_in_bytes (type);
2964
2965   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
2966     return 0;
2967
2968   if (VECTOR_MODE_P (mode) || mode == TImode)
2969     {
2970       /* User-created vectors small enough to fit in EAX.  */
2971       if (size < 8)
2972         return 0;
2973
2974       /* MMX/3dNow values are returned on the stack, since we've
2975          got to EMMS/FEMMS before returning.  */
2976       if (size == 8)
2977         return 1;
2978
2979       /* SSE values are returned in XMM0, except when it doesn't exist.  */
2980       if (size == 16)
2981         return (TARGET_SSE ? 0 : 1);
2982     }
2983
2984   if (mode == XFmode)
2985     return 0;
2986
2987   if (size > 12)
2988     return 1;
2989   return 0;
2990 }
2991
2992 /* When returning SSE vector types, we have a choice of either
2993      (1) being abi incompatible with a -march switch, or
2994      (2) generating an error.
2995    Given no good solution, I think the safest thing is one warning.
2996    The user won't be able to use -Werror, but....
2997
2998    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
2999    called in response to actually generating a caller or callee that
3000    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
3001    via aggregate_value_p for general type probing from tree-ssa.  */
3002
3003 static rtx
3004 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
3005 {
3006   static bool warned;
3007
3008   if (!TARGET_SSE && type && !warned)
3009     {
3010       /* Look at the return type of the function, not the function type.  */
3011       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
3012
3013       if (mode == TImode
3014           || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3015         {
3016           warned = true;
3017           warning ("SSE vector return without SSE enabled changes the ABI");
3018         }
3019     }
3020
3021   return NULL;
3022 }
3023
3024 /* Define how to find the value returned by a library function
3025    assuming the value has mode MODE.  */
3026 rtx
3027 ix86_libcall_value (enum machine_mode mode)
3028 {
3029   if (TARGET_64BIT)
3030     {
3031       switch (mode)
3032         {
3033         case SFmode:
3034         case SCmode:
3035         case DFmode:
3036         case DCmode:
3037         case TFmode:
3038           return gen_rtx_REG (mode, FIRST_SSE_REG);
3039         case XFmode:
3040           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
3041         case XCmode:
3042         case TCmode:
3043           return NULL;
3044         default:
3045           return gen_rtx_REG (mode, 0);
3046         }
3047     }
3048   else
3049     return gen_rtx_REG (mode, ix86_value_regno (mode));
3050 }
3051
3052 /* Given a mode, return the register to use for a return value.  */
3053
3054 static int
3055 ix86_value_regno (enum machine_mode mode)
3056 {
3057   /* Floating point return values in %st(0).  */
3058   if (GET_MODE_CLASS (mode) == MODE_FLOAT && TARGET_FLOAT_RETURNS_IN_80387)
3059     return FIRST_FLOAT_REG;
3060   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
3061      we prevent this case when sse is not available.  */
3062   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3063     return FIRST_SSE_REG;
3064   /* Everything else in %eax.  */
3065   return 0;
3066 }
3067 \f
3068 /* Create the va_list data type.  */
3069
3070 static tree
3071 ix86_build_builtin_va_list (void)
3072 {
3073   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
3074
3075   /* For i386 we use plain pointer to argument area.  */
3076   if (!TARGET_64BIT)
3077     return build_pointer_type (char_type_node);
3078
3079   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3080   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
3081
3082   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
3083                       unsigned_type_node);
3084   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
3085                       unsigned_type_node);
3086   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
3087                       ptr_type_node);
3088   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
3089                       ptr_type_node);
3090
3091   DECL_FIELD_CONTEXT (f_gpr) = record;
3092   DECL_FIELD_CONTEXT (f_fpr) = record;
3093   DECL_FIELD_CONTEXT (f_ovf) = record;
3094   DECL_FIELD_CONTEXT (f_sav) = record;
3095
3096   TREE_CHAIN (record) = type_decl;
3097   TYPE_NAME (record) = type_decl;
3098   TYPE_FIELDS (record) = f_gpr;
3099   TREE_CHAIN (f_gpr) = f_fpr;
3100   TREE_CHAIN (f_fpr) = f_ovf;
3101   TREE_CHAIN (f_ovf) = f_sav;
3102
3103   layout_type (record);
3104
3105   /* The correct type is an array type of one element.  */
3106   return build_array_type (record, build_index_type (size_zero_node));
3107 }
3108
3109 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
3110
3111 static void
3112 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3113                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
3114                              int no_rtl)
3115 {
3116   CUMULATIVE_ARGS next_cum;
3117   rtx save_area = NULL_RTX, mem;
3118   rtx label;
3119   rtx label_ref;
3120   rtx tmp_reg;
3121   rtx nsse_reg;
3122   int set;
3123   tree fntype;
3124   int stdarg_p;
3125   int i;
3126
3127   if (!TARGET_64BIT)
3128     return;
3129
3130   /* Indicate to allocate space on the stack for varargs save area.  */
3131   ix86_save_varrargs_registers = 1;
3132
3133   cfun->stack_alignment_needed = 128;
3134
3135   fntype = TREE_TYPE (current_function_decl);
3136   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
3137               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3138                   != void_type_node));
3139
3140   /* For varargs, we do not want to skip the dummy va_dcl argument.
3141      For stdargs, we do want to skip the last named argument.  */
3142   next_cum = *cum;
3143   if (stdarg_p)
3144     function_arg_advance (&next_cum, mode, type, 1);
3145
3146   if (!no_rtl)
3147     save_area = frame_pointer_rtx;
3148
3149   set = get_varargs_alias_set ();
3150
3151   for (i = next_cum.regno; i < ix86_regparm; i++)
3152     {
3153       mem = gen_rtx_MEM (Pmode,
3154                          plus_constant (save_area, i * UNITS_PER_WORD));
3155       set_mem_alias_set (mem, set);
3156       emit_move_insn (mem, gen_rtx_REG (Pmode,
3157                                         x86_64_int_parameter_registers[i]));
3158     }
3159
3160   if (next_cum.sse_nregs)
3161     {
3162       /* Now emit code to save SSE registers.  The AX parameter contains number
3163          of SSE parameter registers used to call this function.  We use
3164          sse_prologue_save insn template that produces computed jump across
3165          SSE saves.  We need some preparation work to get this working.  */
3166
3167       label = gen_label_rtx ();
3168       label_ref = gen_rtx_LABEL_REF (Pmode, label);
3169
3170       /* Compute address to jump to :
3171          label - 5*eax + nnamed_sse_arguments*5  */
3172       tmp_reg = gen_reg_rtx (Pmode);
3173       nsse_reg = gen_reg_rtx (Pmode);
3174       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
3175       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3176                               gen_rtx_MULT (Pmode, nsse_reg,
3177                                             GEN_INT (4))));
3178       if (next_cum.sse_regno)
3179         emit_move_insn
3180           (nsse_reg,
3181            gen_rtx_CONST (DImode,
3182                           gen_rtx_PLUS (DImode,
3183                                         label_ref,
3184                                         GEN_INT (next_cum.sse_regno * 4))));
3185       else
3186         emit_move_insn (nsse_reg, label_ref);
3187       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
3188
3189       /* Compute address of memory block we save into.  We always use pointer
3190          pointing 127 bytes after first byte to store - this is needed to keep
3191          instruction size limited by 4 bytes.  */
3192       tmp_reg = gen_reg_rtx (Pmode);
3193       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3194                               plus_constant (save_area,
3195                                              8 * REGPARM_MAX + 127)));
3196       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
3197       set_mem_alias_set (mem, set);
3198       set_mem_align (mem, BITS_PER_WORD);
3199
3200       /* And finally do the dirty job!  */
3201       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
3202                                         GEN_INT (next_cum.sse_regno), label));
3203     }
3204
3205 }
3206
3207 /* Implement va_start.  */
3208
3209 void
3210 ix86_va_start (tree valist, rtx nextarg)
3211 {
3212   HOST_WIDE_INT words, n_gpr, n_fpr;
3213   tree f_gpr, f_fpr, f_ovf, f_sav;
3214   tree gpr, fpr, ovf, sav, t;
3215
3216   /* Only 64bit target needs something special.  */
3217   if (!TARGET_64BIT)
3218     {
3219       std_expand_builtin_va_start (valist, nextarg);
3220       return;
3221     }
3222
3223   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3224   f_fpr = TREE_CHAIN (f_gpr);
3225   f_ovf = TREE_CHAIN (f_fpr);
3226   f_sav = TREE_CHAIN (f_ovf);
3227
3228   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3229   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
3230   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
3231   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
3232   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
3233
3234   /* Count number of gp and fp argument registers used.  */
3235   words = current_function_args_info.words;
3236   n_gpr = current_function_args_info.regno;
3237   n_fpr = current_function_args_info.sse_regno;
3238
3239   if (TARGET_DEBUG_ARG)
3240     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
3241              (int) words, (int) n_gpr, (int) n_fpr);
3242
3243   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
3244              build_int_cst (NULL_TREE, n_gpr * 8));
3245   TREE_SIDE_EFFECTS (t) = 1;
3246   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3247
3248   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
3249              build_int_cst (NULL_TREE, n_fpr * 16 + 8*REGPARM_MAX));
3250   TREE_SIDE_EFFECTS (t) = 1;
3251   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3252
3253   /* Find the overflow area.  */
3254   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
3255   if (words != 0)
3256     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
3257                build_int_cst (NULL_TREE, words * UNITS_PER_WORD));
3258   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3259   TREE_SIDE_EFFECTS (t) = 1;
3260   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3261
3262   /* Find the register save area.
3263      Prologue of the function save it right above stack frame.  */
3264   t = make_tree (TREE_TYPE (sav), frame_pointer_rtx);
3265   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
3266   TREE_SIDE_EFFECTS (t) = 1;
3267   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3268 }
3269
3270 /* Implement va_arg.  */
3271
3272 tree
3273 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
3274 {
3275   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
3276   tree f_gpr, f_fpr, f_ovf, f_sav;
3277   tree gpr, fpr, ovf, sav, t;
3278   int size, rsize;
3279   tree lab_false, lab_over = NULL_TREE;
3280   tree addr, t2;
3281   rtx container;
3282   int indirect_p = 0;
3283   tree ptrtype;
3284
3285   /* Only 64bit target needs something special.  */
3286   if (!TARGET_64BIT)
3287     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
3288
3289   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3290   f_fpr = TREE_CHAIN (f_gpr);
3291   f_ovf = TREE_CHAIN (f_fpr);
3292   f_sav = TREE_CHAIN (f_ovf);
3293
3294   valist = build_fold_indirect_ref (valist);
3295   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
3296   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
3297   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
3298   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
3299
3300   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3301   if (indirect_p)
3302     type = build_pointer_type (type);
3303   size = int_size_in_bytes (type);
3304   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3305
3306   container = construct_container (TYPE_MODE (type), type, 0,
3307                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
3308   /*
3309    * Pull the value out of the saved registers ...
3310    */
3311
3312   addr = create_tmp_var (ptr_type_node, "addr");
3313   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
3314
3315   if (container)
3316     {
3317       int needed_intregs, needed_sseregs;
3318       bool need_temp;
3319       tree int_addr, sse_addr;
3320
3321       lab_false = create_artificial_label ();
3322       lab_over = create_artificial_label ();
3323
3324       examine_argument (TYPE_MODE (type), type, 0,
3325                         &needed_intregs, &needed_sseregs);
3326
3327       need_temp = (!REG_P (container)
3328                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
3329                        || TYPE_ALIGN (type) > 128));
3330
3331       /* In case we are passing structure, verify that it is consecutive block
3332          on the register save area.  If not we need to do moves.  */
3333       if (!need_temp && !REG_P (container))
3334         {
3335           /* Verify that all registers are strictly consecutive  */
3336           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
3337             {
3338               int i;
3339
3340               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3341                 {
3342                   rtx slot = XVECEXP (container, 0, i);
3343                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
3344                       || INTVAL (XEXP (slot, 1)) != i * 16)
3345                     need_temp = 1;
3346                 }
3347             }
3348           else
3349             {
3350               int i;
3351
3352               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3353                 {
3354                   rtx slot = XVECEXP (container, 0, i);
3355                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
3356                       || INTVAL (XEXP (slot, 1)) != i * 8)
3357                     need_temp = 1;
3358                 }
3359             }
3360         }
3361       if (!need_temp)
3362         {
3363           int_addr = addr;
3364           sse_addr = addr;
3365         }
3366       else
3367         {
3368           int_addr = create_tmp_var (ptr_type_node, "int_addr");
3369           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
3370           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
3371           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
3372         }
3373       /* First ensure that we fit completely in registers.  */
3374       if (needed_intregs)
3375         {
3376           t = build_int_cst (TREE_TYPE (gpr),
3377                              (REGPARM_MAX - needed_intregs + 1) * 8);
3378           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
3379           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
3380           t = build (COND_EXPR, void_type_node, t, t2, NULL_TREE);
3381           gimplify_and_add (t, pre_p);
3382         }
3383       if (needed_sseregs)
3384         {
3385           t = build_int_cst (TREE_TYPE (fpr),
3386                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
3387                              + REGPARM_MAX * 8);
3388           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
3389           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
3390           t = build (COND_EXPR, void_type_node, t, t2, NULL_TREE);
3391           gimplify_and_add (t, pre_p);
3392         }
3393
3394       /* Compute index to start of area used for integer regs.  */
3395       if (needed_intregs)
3396         {
3397           /* int_addr = gpr + sav; */
3398           t = build2 (PLUS_EXPR, ptr_type_node, sav, gpr);
3399           t = build2 (MODIFY_EXPR, void_type_node, int_addr, t);
3400           gimplify_and_add (t, pre_p);
3401         }
3402       if (needed_sseregs)
3403         {
3404           /* sse_addr = fpr + sav; */
3405           t = build2 (PLUS_EXPR, ptr_type_node, sav, fpr);
3406           t = build2 (MODIFY_EXPR, void_type_node, sse_addr, t);
3407           gimplify_and_add (t, pre_p);
3408         }
3409       if (need_temp)
3410         {
3411           int i;
3412           tree temp = create_tmp_var (type, "va_arg_tmp");
3413
3414           /* addr = &temp; */
3415           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
3416           t = build2 (MODIFY_EXPR, void_type_node, addr, t);
3417           gimplify_and_add (t, pre_p);
3418
3419           for (i = 0; i < XVECLEN (container, 0); i++)
3420             {
3421               rtx slot = XVECEXP (container, 0, i);
3422               rtx reg = XEXP (slot, 0);
3423               enum machine_mode mode = GET_MODE (reg);
3424               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
3425               tree addr_type = build_pointer_type (piece_type);
3426               tree src_addr, src;
3427               int src_offset;
3428               tree dest_addr, dest;
3429
3430               if (SSE_REGNO_P (REGNO (reg)))
3431                 {
3432                   src_addr = sse_addr;
3433                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
3434                 }
3435               else
3436                 {
3437                   src_addr = int_addr;
3438                   src_offset = REGNO (reg) * 8;
3439                 }
3440               src_addr = fold_convert (addr_type, src_addr);
3441               src_addr = fold (build2 (PLUS_EXPR, addr_type, src_addr,
3442                                        size_int (src_offset)));
3443               src = build_fold_indirect_ref (src_addr);
3444
3445               dest_addr = fold_convert (addr_type, addr);
3446               dest_addr = fold (build2 (PLUS_EXPR, addr_type, dest_addr,
3447                                         size_int (INTVAL (XEXP (slot, 1)))));
3448               dest = build_fold_indirect_ref (dest_addr);
3449
3450               t = build2 (MODIFY_EXPR, void_type_node, dest, src);
3451               gimplify_and_add (t, pre_p);
3452             }
3453         }
3454
3455       if (needed_intregs)
3456         {
3457           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
3458                       build_int_cst (NULL_TREE, needed_intregs * 8));
3459           t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
3460           gimplify_and_add (t, pre_p);
3461         }
3462       if (needed_sseregs)
3463         {
3464           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
3465                       build_int_cst (NULL_TREE, needed_sseregs * 16));
3466           t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
3467           gimplify_and_add (t, pre_p);
3468         }
3469
3470       t = build1 (GOTO_EXPR, void_type_node, lab_over);
3471       gimplify_and_add (t, pre_p);
3472
3473       t = build1 (LABEL_EXPR, void_type_node, lab_false);
3474       append_to_statement_list (t, pre_p);
3475     }
3476
3477   /* ... otherwise out of the overflow area.  */
3478
3479   /* Care for on-stack alignment if needed.  */
3480   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64)
3481     t = ovf;
3482   else
3483     {
3484       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
3485       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf,
3486                  build_int_cst (NULL_TREE, align - 1));
3487       t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
3488                  build_int_cst (NULL_TREE, -align));
3489     }
3490   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
3491
3492   t2 = build2 (MODIFY_EXPR, void_type_node, addr, t);
3493   gimplify_and_add (t2, pre_p);
3494
3495   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
3496               build_int_cst (NULL_TREE, rsize * UNITS_PER_WORD));
3497   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3498   gimplify_and_add (t, pre_p);
3499
3500   if (container)
3501     {
3502       t = build1 (LABEL_EXPR, void_type_node, lab_over);
3503       append_to_statement_list (t, pre_p);
3504     }
3505
3506   ptrtype = build_pointer_type (type);
3507   addr = fold_convert (ptrtype, addr);
3508
3509   if (indirect_p)
3510     addr = build_fold_indirect_ref (addr);
3511   return build_fold_indirect_ref (addr);
3512 }
3513 \f
3514 /* Return nonzero if OPNUM's MEM should be matched
3515    in movabs* patterns.  */
3516
3517 int
3518 ix86_check_movabs (rtx insn, int opnum)
3519 {
3520   rtx set, mem;
3521
3522   set = PATTERN (insn);
3523   if (GET_CODE (set) == PARALLEL)
3524     set = XVECEXP (set, 0, 0);
3525   if (GET_CODE (set) != SET)
3526     abort ();
3527   mem = XEXP (set, opnum);
3528   while (GET_CODE (mem) == SUBREG)
3529     mem = SUBREG_REG (mem);
3530   if (GET_CODE (mem) != MEM)
3531     abort ();
3532   return (volatile_ok || !MEM_VOLATILE_P (mem));
3533 }
3534 \f
3535 /* Initialize the table of extra 80387 mathematical constants.  */
3536
3537 static void
3538 init_ext_80387_constants (void)
3539 {
3540   static const char * cst[5] =
3541   {
3542     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
3543     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
3544     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
3545     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
3546     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
3547   };
3548   int i;
3549
3550   for (i = 0; i < 5; i++)
3551     {
3552       real_from_string (&ext_80387_constants_table[i], cst[i]);
3553       /* Ensure each constant is rounded to XFmode precision.  */
3554       real_convert (&ext_80387_constants_table[i],
3555                     XFmode, &ext_80387_constants_table[i]);
3556     }
3557
3558   ext_80387_constants_init = 1;
3559 }
3560
3561 /* Return true if the constant is something that can be loaded with
3562    a special instruction.  */
3563
3564 int
3565 standard_80387_constant_p (rtx x)
3566 {
3567   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
3568     return -1;
3569
3570   if (x == CONST0_RTX (GET_MODE (x)))
3571     return 1;
3572   if (x == CONST1_RTX (GET_MODE (x)))
3573     return 2;
3574
3575   /* For XFmode constants, try to find a special 80387 instruction when
3576      optimizing for size or on those CPUs that benefit from them.  */
3577   if (GET_MODE (x) == XFmode
3578       && (optimize_size || x86_ext_80387_constants & TUNEMASK))
3579     {
3580       REAL_VALUE_TYPE r;
3581       int i;
3582
3583       if (! ext_80387_constants_init)
3584         init_ext_80387_constants ();
3585
3586       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3587       for (i = 0; i < 5; i++)
3588         if (real_identical (&r, &ext_80387_constants_table[i]))
3589           return i + 3;
3590     }
3591
3592   return 0;
3593 }
3594
3595 /* Return the opcode of the special instruction to be used to load
3596    the constant X.  */
3597
3598 const char *
3599 standard_80387_constant_opcode (rtx x)
3600 {
3601   switch (standard_80387_constant_p (x))
3602     {
3603     case 1:
3604       return "fldz";
3605     case 2:
3606       return "fld1";
3607     case 3:
3608       return "fldlg2";
3609     case 4:
3610       return "fldln2";
3611     case 5:
3612       return "fldl2e";
3613     case 6:
3614       return "fldl2t";
3615     case 7:
3616       return "fldpi";
3617     }
3618   abort ();
3619 }
3620
3621 /* Return the CONST_DOUBLE representing the 80387 constant that is
3622    loaded by the specified special instruction.  The argument IDX
3623    matches the return value from standard_80387_constant_p.  */
3624
3625 rtx
3626 standard_80387_constant_rtx (int idx)
3627 {
3628   int i;
3629
3630   if (! ext_80387_constants_init)
3631     init_ext_80387_constants ();
3632
3633   switch (idx)
3634     {
3635     case 3:
3636     case 4:
3637     case 5:
3638     case 6:
3639     case 7:
3640       i = idx - 3;
3641       break;
3642
3643     default:
3644       abort ();
3645     }
3646
3647   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
3648                                        XFmode);
3649 }
3650
3651 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
3652  */
3653 int
3654 standard_sse_constant_p (rtx x)
3655 {
3656   if (x == const0_rtx)
3657     return 1;
3658   return (x == CONST0_RTX (GET_MODE (x)));
3659 }
3660
3661 /* Returns 1 if OP contains a symbol reference */
3662
3663 int
3664 symbolic_reference_mentioned_p (rtx op)
3665 {
3666   const char *fmt;
3667   int i;
3668
3669   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
3670     return 1;
3671
3672   fmt = GET_RTX_FORMAT (GET_CODE (op));
3673   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
3674     {
3675       if (fmt[i] == 'E')
3676         {
3677           int j;
3678
3679           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
3680             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
3681               return 1;
3682         }
3683
3684       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
3685         return 1;
3686     }
3687
3688   return 0;
3689 }
3690
3691 /* Return 1 if it is appropriate to emit `ret' instructions in the
3692    body of a function.  Do this only if the epilogue is simple, needing a
3693    couple of insns.  Prior to reloading, we can't tell how many registers
3694    must be saved, so return 0 then.  Return 0 if there is no frame
3695    marker to de-allocate.
3696
3697    If NON_SAVING_SETJMP is defined and true, then it is not possible
3698    for the epilogue to be simple, so return 0.  This is a special case
3699    since NON_SAVING_SETJMP will not cause regs_ever_live to change
3700    until final, but jump_optimize may need to know sooner if a
3701    `return' is OK.  */
3702
3703 int
3704 ix86_can_use_return_insn_p (void)
3705 {
3706   struct ix86_frame frame;
3707
3708 #ifdef NON_SAVING_SETJMP
3709   if (NON_SAVING_SETJMP && current_function_calls_setjmp)
3710     return 0;
3711 #endif
3712
3713   if (! reload_completed || frame_pointer_needed)
3714     return 0;
3715
3716   /* Don't allow more than 32 pop, since that's all we can do
3717      with one instruction.  */
3718   if (current_function_pops_args
3719       && current_function_args_size >= 32768)
3720     return 0;
3721
3722   ix86_compute_frame_layout (&frame);
3723   return frame.to_allocate == 0 && frame.nregs == 0;
3724 }
3725 \f
3726 /* Value should be nonzero if functions must have frame pointers.
3727    Zero means the frame pointer need not be set up (and parms may
3728    be accessed via the stack pointer) in functions that seem suitable.  */
3729
3730 int
3731 ix86_frame_pointer_required (void)
3732 {
3733   /* If we accessed previous frames, then the generated code expects
3734      to be able to access the saved ebp value in our frame.  */
3735   if (cfun->machine->accesses_prev_frame)
3736     return 1;
3737
3738   /* Several x86 os'es need a frame pointer for other reasons,
3739      usually pertaining to setjmp.  */
3740   if (SUBTARGET_FRAME_POINTER_REQUIRED)
3741     return 1;
3742
3743   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
3744      the frame pointer by default.  Turn it back on now if we've not
3745      got a leaf function.  */
3746   if (TARGET_OMIT_LEAF_FRAME_POINTER
3747       && (!current_function_is_leaf))
3748     return 1;
3749
3750   if (current_function_profile)
3751     return 1;
3752
3753   return 0;
3754 }
3755
3756 /* Record that the current function accesses previous call frames.  */
3757
3758 void
3759 ix86_setup_frame_addresses (void)
3760 {
3761   cfun->machine->accesses_prev_frame = 1;
3762 }
3763 \f
3764 #if defined(HAVE_GAS_HIDDEN) && defined(SUPPORTS_ONE_ONLY)
3765 # define USE_HIDDEN_LINKONCE 1
3766 #else
3767 # define USE_HIDDEN_LINKONCE 0
3768 #endif
3769
3770 static int pic_labels_used;
3771
3772 /* Fills in the label name that should be used for a pc thunk for
3773    the given register.  */
3774
3775 static void
3776 get_pc_thunk_name (char name[32], unsigned int regno)
3777 {
3778   if (USE_HIDDEN_LINKONCE)
3779     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
3780   else
3781     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
3782 }
3783
3784
3785 /* This function generates code for -fpic that loads %ebx with
3786    the return address of the caller and then returns.  */
3787
3788 void
3789 ix86_file_end (void)
3790 {
3791   rtx xops[2];
3792   int regno;
3793
3794   for (regno = 0; regno < 8; ++regno)
3795     {
3796       char name[32];
3797
3798       if (! ((pic_labels_used >> regno) & 1))
3799         continue;
3800
3801       get_pc_thunk_name (name, regno);
3802
3803       if (USE_HIDDEN_LINKONCE)
3804         {
3805           tree decl;
3806
3807           decl = build_decl (FUNCTION_DECL, get_identifier (name),
3808                              error_mark_node);
3809           TREE_PUBLIC (decl) = 1;
3810           TREE_STATIC (decl) = 1;
3811           DECL_ONE_ONLY (decl) = 1;
3812
3813           (*targetm.asm_out.unique_section) (decl, 0);
3814           named_section (decl, NULL, 0);
3815
3816           (*targetm.asm_out.globalize_label) (asm_out_file, name);
3817           fputs ("\t.hidden\t", asm_out_file);
3818           assemble_name (asm_out_file, name);
3819           fputc ('\n', asm_out_file);
3820           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
3821         }
3822       else
3823         {
3824           text_section ();
3825           ASM_OUTPUT_LABEL (asm_out_file, name);
3826         }
3827
3828       xops[0] = gen_rtx_REG (SImode, regno);
3829       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
3830       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
3831       output_asm_insn ("ret", xops);
3832     }
3833
3834   if (NEED_INDICATE_EXEC_STACK)
3835     file_end_indicate_exec_stack ();
3836 }
3837
3838 /* Emit code for the SET_GOT patterns.  */
3839
3840 const char *
3841 output_set_got (rtx dest)
3842 {
3843   rtx xops[3];
3844
3845   xops[0] = dest;
3846   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
3847
3848   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
3849     {
3850       xops[2] = gen_rtx_LABEL_REF (Pmode, gen_label_rtx ());
3851
3852       if (!flag_pic)
3853         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
3854       else
3855         output_asm_insn ("call\t%a2", xops);
3856
3857 #if TARGET_MACHO
3858       /* Output the "canonical" label name ("Lxx$pb") here too.  This
3859          is what will be referred to by the Mach-O PIC subsystem.  */
3860       ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
3861 #endif
3862       (*targetm.asm_out.internal_label) (asm_out_file, "L",
3863                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
3864
3865       if (flag_pic)
3866         output_asm_insn ("pop{l}\t%0", xops);
3867     }
3868   else
3869     {
3870       char name[32];
3871       get_pc_thunk_name (name, REGNO (dest));
3872       pic_labels_used |= 1 << REGNO (dest);
3873
3874       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
3875       xops[2] = gen_rtx_MEM (QImode, xops[2]);
3876       output_asm_insn ("call\t%X2", xops);
3877     }
3878
3879   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
3880     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
3881   else if (!TARGET_MACHO)
3882     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %a1+(.-%a2)}", xops);
3883
3884   return "";
3885 }
3886
3887 /* Generate an "push" pattern for input ARG.  */
3888
3889 static rtx
3890 gen_push (rtx arg)
3891 {
3892   return gen_rtx_SET (VOIDmode,
3893                       gen_rtx_MEM (Pmode,
3894                                    gen_rtx_PRE_DEC (Pmode,
3895                                                     stack_pointer_rtx)),
3896                       arg);
3897 }
3898
3899 /* Return >= 0 if there is an unused call-clobbered register available
3900    for the entire function.  */
3901
3902 static unsigned int
3903 ix86_select_alt_pic_regnum (void)
3904 {
3905   if (current_function_is_leaf && !current_function_profile)
3906     {
3907       int i;
3908       for (i = 2; i >= 0; --i)
3909         if (!regs_ever_live[i])
3910           return i;
3911     }
3912
3913   return INVALID_REGNUM;
3914 }
3915
3916 /* Return 1 if we need to save REGNO.  */
3917 static int
3918 ix86_save_reg (unsigned int regno, int maybe_eh_return)
3919 {
3920   if (pic_offset_table_rtx
3921       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
3922       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
3923           || current_function_profile
3924           || current_function_calls_eh_return
3925           || current_function_uses_const_pool))
3926     {
3927       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
3928         return 0;
3929       return 1;
3930     }
3931
3932   if (current_function_calls_eh_return && maybe_eh_return)
3933     {
3934       unsigned i;
3935       for (i = 0; ; i++)
3936         {
3937           unsigned test = EH_RETURN_DATA_REGNO (i);
3938           if (test == INVALID_REGNUM)
3939             break;
3940           if (test == regno)
3941             return 1;
3942         }
3943     }
3944
3945   return (regs_ever_live[regno]
3946           && !call_used_regs[regno]
3947           && !fixed_regs[regno]
3948           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
3949 }
3950
3951 /* Return number of registers to be saved on the stack.  */
3952
3953 static int
3954 ix86_nsaved_regs (void)
3955 {
3956   int nregs = 0;
3957   int regno;
3958
3959   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
3960     if (ix86_save_reg (regno, true))
3961       nregs++;
3962   return nregs;
3963 }
3964
3965 /* Return the offset between two registers, one to be eliminated, and the other
3966    its replacement, at the start of a routine.  */
3967
3968 HOST_WIDE_INT
3969 ix86_initial_elimination_offset (int from, int to)
3970 {
3971   struct ix86_frame frame;
3972   ix86_compute_frame_layout (&frame);
3973
3974   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
3975     return frame.hard_frame_pointer_offset;
3976   else if (from == FRAME_POINTER_REGNUM
3977            && to == HARD_FRAME_POINTER_REGNUM)
3978     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
3979   else
3980     {
3981       if (to != STACK_POINTER_REGNUM)
3982         abort ();
3983       else if (from == ARG_POINTER_REGNUM)
3984         return frame.stack_pointer_offset;
3985       else if (from != FRAME_POINTER_REGNUM)
3986         abort ();
3987       else
3988         return frame.stack_pointer_offset - frame.frame_pointer_offset;
3989     }
3990 }
3991
3992 /* Fill structure ix86_frame about frame of currently computed function.  */
3993
3994 static void
3995 ix86_compute_frame_layout (struct ix86_frame *frame)
3996 {
3997   HOST_WIDE_INT total_size;
3998   unsigned int stack_alignment_needed;
3999   HOST_WIDE_INT offset;
4000   unsigned int preferred_alignment;
4001   HOST_WIDE_INT size = get_frame_size ();
4002
4003   frame->nregs = ix86_nsaved_regs ();
4004   total_size = size;
4005
4006   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
4007   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
4008
4009   /* During reload iteration the amount of registers saved can change.
4010      Recompute the value as needed.  Do not recompute when amount of registers
4011      didn't change as reload does mutiple calls to the function and does not
4012      expect the decision to change within single iteration.  */
4013   if (!optimize_size
4014       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
4015     {
4016       int count = frame->nregs;
4017
4018       cfun->machine->use_fast_prologue_epilogue_nregs = count;
4019       /* The fast prologue uses move instead of push to save registers.  This
4020          is significantly longer, but also executes faster as modern hardware
4021          can execute the moves in parallel, but can't do that for push/pop.
4022
4023          Be careful about choosing what prologue to emit:  When function takes
4024          many instructions to execute we may use slow version as well as in
4025          case function is known to be outside hot spot (this is known with
4026          feedback only).  Weight the size of function by number of registers
4027          to save as it is cheap to use one or two push instructions but very
4028          slow to use many of them.  */
4029       if (count)
4030         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
4031       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
4032           || (flag_branch_probabilities
4033               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
4034         cfun->machine->use_fast_prologue_epilogue = false;
4035       else
4036         cfun->machine->use_fast_prologue_epilogue
4037            = !expensive_function_p (count);
4038     }
4039   if (TARGET_PROLOGUE_USING_MOVE
4040       && cfun->machine->use_fast_prologue_epilogue)
4041     frame->save_regs_using_mov = true;
4042   else
4043     frame->save_regs_using_mov = false;
4044
4045
4046   /* Skip return address and saved base pointer.  */
4047   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
4048
4049   frame->hard_frame_pointer_offset = offset;
4050
4051   /* Do some sanity checking of stack_alignment_needed and
4052      preferred_alignment, since i386 port is the only using those features
4053      that may break easily.  */
4054
4055   if (size && !stack_alignment_needed)
4056     abort ();
4057   if (preferred_alignment < STACK_BOUNDARY / BITS_PER_UNIT)
4058     abort ();
4059   if (preferred_alignment > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
4060     abort ();
4061   if (stack_alignment_needed > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
4062     abort ();
4063
4064   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
4065     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
4066
4067   /* Register save area */
4068   offset += frame->nregs * UNITS_PER_WORD;
4069
4070   /* Va-arg area */
4071   if (ix86_save_varrargs_registers)
4072     {
4073       offset += X86_64_VARARGS_SIZE;
4074       frame->va_arg_size = X86_64_VARARGS_SIZE;
4075     }
4076   else
4077     frame->va_arg_size = 0;
4078
4079   /* Align start of frame for local function.  */
4080   frame->padding1 = ((offset + stack_alignment_needed - 1)
4081                      & -stack_alignment_needed) - offset;
4082
4083   offset += frame->padding1;
4084
4085   /* Frame pointer points here.  */
4086   frame->frame_pointer_offset = offset;
4087
4088   offset += size;
4089
4090   /* Add outgoing arguments area.  Can be skipped if we eliminated
4091      all the function calls as dead code.
4092      Skipping is however impossible when function calls alloca.  Alloca
4093      expander assumes that last current_function_outgoing_args_size
4094      of stack frame are unused.  */
4095   if (ACCUMULATE_OUTGOING_ARGS
4096       && (!current_function_is_leaf || current_function_calls_alloca))
4097     {
4098       offset += current_function_outgoing_args_size;
4099       frame->outgoing_arguments_size = current_function_outgoing_args_size;
4100     }
4101   else
4102     frame->outgoing_arguments_size = 0;
4103
4104   /* Align stack boundary.  Only needed if we're calling another function
4105      or using alloca.  */
4106   if (!current_function_is_leaf || current_function_calls_alloca)
4107     frame->padding2 = ((offset + preferred_alignment - 1)
4108                        & -preferred_alignment) - offset;
4109   else
4110     frame->padding2 = 0;
4111
4112   offset += frame->padding2;
4113
4114   /* We've reached end of stack frame.  */
4115   frame->stack_pointer_offset = offset;
4116
4117   /* Size prologue needs to allocate.  */
4118   frame->to_allocate =
4119     (size + frame->padding1 + frame->padding2
4120      + frame->outgoing_arguments_size + frame->va_arg_size);
4121
4122   if ((!frame->to_allocate && frame->nregs <= 1)
4123       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
4124     frame->save_regs_using_mov = false;
4125
4126   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
4127       && current_function_is_leaf)
4128     {
4129       frame->red_zone_size = frame->to_allocate;
4130       if (frame->save_regs_using_mov)
4131         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
4132       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
4133         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
4134     }
4135   else
4136     frame->red_zone_size = 0;
4137   frame->to_allocate -= frame->red_zone_size;
4138   frame->stack_pointer_offset -= frame->red_zone_size;
4139 #if 0
4140   fprintf (stderr, "nregs: %i\n", frame->nregs);
4141   fprintf (stderr, "size: %i\n", size);
4142   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
4143   fprintf (stderr, "padding1: %i\n", frame->padding1);
4144   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
4145   fprintf (stderr, "padding2: %i\n", frame->padding2);
4146   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
4147   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
4148   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
4149   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
4150            frame->hard_frame_pointer_offset);
4151   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
4152 #endif
4153 }
4154
4155 /* Emit code to save registers in the prologue.  */
4156
4157 static void
4158 ix86_emit_save_regs (void)
4159 {
4160   int regno;
4161   rtx insn;
4162
4163   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4164     if (ix86_save_reg (regno, true))
4165       {
4166         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
4167         RTX_FRAME_RELATED_P (insn) = 1;
4168       }
4169 }
4170
4171 /* Emit code to save registers using MOV insns.  First register
4172    is restored from POINTER + OFFSET.  */
4173 static void
4174 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
4175 {
4176   int regno;
4177   rtx insn;
4178
4179   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4180     if (ix86_save_reg (regno, true))
4181       {
4182         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
4183                                                Pmode, offset),
4184                                gen_rtx_REG (Pmode, regno));
4185         RTX_FRAME_RELATED_P (insn) = 1;
4186         offset += UNITS_PER_WORD;
4187       }
4188 }
4189
4190 /* Expand prologue or epilogue stack adjustment.
4191    The pattern exist to put a dependency on all ebp-based memory accesses.
4192    STYLE should be negative if instructions should be marked as frame related,
4193    zero if %r11 register is live and cannot be freely used and positive
4194    otherwise.  */
4195
4196 static void
4197 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
4198 {
4199   rtx insn;
4200
4201   if (! TARGET_64BIT)
4202     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
4203   else if (x86_64_immediate_operand (offset, DImode))
4204     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
4205   else
4206     {
4207       rtx r11;
4208       /* r11 is used by indirect sibcall return as well, set before the
4209          epilogue and used after the epilogue.  ATM indirect sibcall
4210          shouldn't be used together with huge frame sizes in one
4211          function because of the frame_size check in sibcall.c.  */
4212       if (style == 0)
4213         abort ();
4214       r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
4215       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
4216       if (style < 0)
4217         RTX_FRAME_RELATED_P (insn) = 1;
4218       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
4219                                                                offset));
4220     }
4221   if (style < 0)
4222     RTX_FRAME_RELATED_P (insn) = 1;
4223 }
4224
4225 /* Expand the prologue into a bunch of separate insns.  */
4226
4227 void
4228 ix86_expand_prologue (void)
4229 {
4230   rtx insn;
4231   bool pic_reg_used;
4232   struct ix86_frame frame;
4233   HOST_WIDE_INT allocate;
4234
4235   ix86_compute_frame_layout (&frame);
4236
4237   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
4238      slower on all targets.  Also sdb doesn't like it.  */
4239
4240   if (frame_pointer_needed)
4241     {
4242       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
4243       RTX_FRAME_RELATED_P (insn) = 1;
4244
4245       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
4246       RTX_FRAME_RELATED_P (insn) = 1;
4247     }
4248
4249   allocate = frame.to_allocate;
4250
4251   if (!frame.save_regs_using_mov)
4252     ix86_emit_save_regs ();
4253   else
4254     allocate += frame.nregs * UNITS_PER_WORD;
4255
4256   /* When using red zone we may start register saving before allocating
4257      the stack frame saving one cycle of the prologue.  */
4258   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
4259     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
4260                                    : stack_pointer_rtx,
4261                                    -frame.nregs * UNITS_PER_WORD);
4262
4263   if (allocate == 0)
4264     ;
4265   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
4266     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
4267                                GEN_INT (-allocate), -1);
4268   else
4269     {
4270       /* Only valid for Win32.  */
4271       rtx eax = gen_rtx_REG (SImode, 0);
4272       bool eax_live = ix86_eax_live_at_start_p ();
4273
4274       if (TARGET_64BIT)
4275         abort ();
4276
4277       if (eax_live)
4278         {
4279           emit_insn (gen_push (eax));
4280           allocate -= 4;
4281         }
4282
4283       insn = emit_move_insn (eax, GEN_INT (allocate));
4284       RTX_FRAME_RELATED_P (insn) = 1;
4285
4286       insn = emit_insn (gen_allocate_stack_worker (eax));
4287       RTX_FRAME_RELATED_P (insn) = 1;
4288
4289       if (eax_live)
4290         {
4291           rtx t;
4292           if (frame_pointer_needed)
4293             t = plus_constant (hard_frame_pointer_rtx,
4294                                allocate
4295                                - frame.to_allocate
4296                                - frame.nregs * UNITS_PER_WORD);
4297           else
4298             t = plus_constant (stack_pointer_rtx, allocate);
4299           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
4300         }
4301     }
4302
4303   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
4304     {
4305       if (!frame_pointer_needed || !frame.to_allocate)
4306         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
4307       else
4308         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
4309                                        -frame.nregs * UNITS_PER_WORD);
4310     }
4311
4312   pic_reg_used = false;
4313   if (pic_offset_table_rtx
4314       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4315           || current_function_profile))
4316     {
4317       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
4318
4319       if (alt_pic_reg_used != INVALID_REGNUM)
4320         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
4321
4322       pic_reg_used = true;
4323     }
4324
4325   if (pic_reg_used)
4326     {
4327       insn = emit_insn (gen_set_got (pic_offset_table_rtx));
4328
4329       /* Even with accurate pre-reload life analysis, we can wind up
4330          deleting all references to the pic register after reload.
4331          Consider if cross-jumping unifies two sides of a branch
4332          controlled by a comparison vs the only read from a global.
4333          In which case, allow the set_got to be deleted, though we're
4334          too late to do anything about the ebx save in the prologue.  */
4335       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
4336     }
4337
4338   /* Prevent function calls from be scheduled before the call to mcount.
4339      In the pic_reg_used case, make sure that the got load isn't deleted.  */
4340   if (current_function_profile)
4341     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
4342 }
4343
4344 /* Emit code to restore saved registers using MOV insns.  First register
4345    is restored from POINTER + OFFSET.  */
4346 static void
4347 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
4348                                   int maybe_eh_return)
4349 {
4350   int regno;
4351   rtx base_address = gen_rtx_MEM (Pmode, pointer);
4352
4353   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4354     if (ix86_save_reg (regno, maybe_eh_return))
4355       {
4356         /* Ensure that adjust_address won't be forced to produce pointer
4357            out of range allowed by x86-64 instruction set.  */
4358         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
4359           {
4360             rtx r11;
4361
4362             r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
4363             emit_move_insn (r11, GEN_INT (offset));
4364             emit_insn (gen_adddi3 (r11, r11, pointer));
4365             base_address = gen_rtx_MEM (Pmode, r11);
4366             offset = 0;
4367           }
4368         emit_move_insn (gen_rtx_REG (Pmode, regno),
4369                         adjust_address (base_address, Pmode, offset));
4370         offset += UNITS_PER_WORD;
4371       }
4372 }
4373
4374 /* Restore function stack, frame, and registers.  */
4375
4376 void
4377 ix86_expand_epilogue (int style)
4378 {
4379   int regno;
4380   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
4381   struct ix86_frame frame;
4382   HOST_WIDE_INT offset;
4383
4384   ix86_compute_frame_layout (&frame);
4385
4386   /* Calculate start of saved registers relative to ebp.  Special care
4387      must be taken for the normal return case of a function using
4388      eh_return: the eax and edx registers are marked as saved, but not
4389      restored along this path.  */
4390   offset = frame.nregs;
4391   if (current_function_calls_eh_return && style != 2)
4392     offset -= 2;
4393   offset *= -UNITS_PER_WORD;
4394
4395   /* If we're only restoring one register and sp is not valid then
4396      using a move instruction to restore the register since it's
4397      less work than reloading sp and popping the register.
4398
4399      The default code result in stack adjustment using add/lea instruction,
4400      while this code results in LEAVE instruction (or discrete equivalent),
4401      so it is profitable in some other cases as well.  Especially when there
4402      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
4403      and there is exactly one register to pop. This heuristic may need some
4404      tuning in future.  */
4405   if ((!sp_valid && frame.nregs <= 1)
4406       || (TARGET_EPILOGUE_USING_MOVE
4407           && cfun->machine->use_fast_prologue_epilogue
4408           && (frame.nregs > 1 || frame.to_allocate))
4409       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
4410       || (frame_pointer_needed && TARGET_USE_LEAVE
4411           && cfun->machine->use_fast_prologue_epilogue
4412           && frame.nregs == 1)
4413       || current_function_calls_eh_return)
4414     {
4415       /* Restore registers.  We can use ebp or esp to address the memory
4416          locations.  If both are available, default to ebp, since offsets
4417          are known to be small.  Only exception is esp pointing directly to the
4418          end of block of saved registers, where we may simplify addressing
4419          mode.  */
4420
4421       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
4422         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
4423                                           frame.to_allocate, style == 2);
4424       else
4425         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
4426                                           offset, style == 2);
4427
4428       /* eh_return epilogues need %ecx added to the stack pointer.  */
4429       if (style == 2)
4430         {
4431           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
4432
4433           if (frame_pointer_needed)
4434             {
4435               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
4436               tmp = plus_constant (tmp, UNITS_PER_WORD);
4437               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
4438
4439               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
4440               emit_move_insn (hard_frame_pointer_rtx, tmp);
4441
4442               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
4443                                          const0_rtx, style);
4444             }
4445           else
4446             {
4447               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
4448               tmp = plus_constant (tmp, (frame.to_allocate
4449                                          + frame.nregs * UNITS_PER_WORD));
4450               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
4451             }
4452         }
4453       else if (!frame_pointer_needed)
4454         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
4455                                    GEN_INT (frame.to_allocate
4456                                             + frame.nregs * UNITS_PER_WORD),
4457                                    style);
4458       /* If not an i386, mov & pop is faster than "leave".  */
4459       else if (TARGET_USE_LEAVE || optimize_size
4460                || !cfun->machine->use_fast_prologue_epilogue)
4461         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
4462       else
4463         {
4464           pro_epilogue_adjust_stack (stack_pointer_rtx,
4465                                      hard_frame_pointer_rtx,
4466                                      const0_rtx, style);
4467           if (TARGET_64BIT)
4468             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
4469           else
4470             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
4471         }
4472     }
4473   else
4474     {
4475       /* First step is to deallocate the stack frame so that we can
4476          pop the registers.  */
4477       if (!sp_valid)
4478         {
4479           if (!frame_pointer_needed)
4480             abort ();
4481           pro_epilogue_adjust_stack (stack_pointer_rtx,
4482                                      hard_frame_pointer_rtx,
4483                                      GEN_INT (offset), style);
4484         }
4485       else if (frame.to_allocate)
4486         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
4487                                    GEN_INT (frame.to_allocate), style);
4488
4489       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4490         if (ix86_save_reg (regno, false))
4491           {
4492             if (TARGET_64BIT)
4493               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
4494             else
4495               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
4496           }
4497       if (frame_pointer_needed)
4498         {
4499           /* Leave results in shorter dependency chains on CPUs that are
4500              able to grok it fast.  */
4501           if (TARGET_USE_LEAVE)
4502             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
4503           else if (TARGET_64BIT)
4504             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
4505           else
4506             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
4507         }
4508     }
4509
4510   /* Sibcall epilogues don't want a return instruction.  */
4511   if (style == 0)
4512     return;
4513
4514   if (current_function_pops_args && current_function_args_size)
4515     {
4516       rtx popc = GEN_INT (current_function_pops_args);
4517
4518       /* i386 can only pop 64K bytes.  If asked to pop more, pop
4519          return address, do explicit add, and jump indirectly to the
4520          caller.  */
4521
4522       if (current_function_pops_args >= 65536)
4523         {
4524           rtx ecx = gen_rtx_REG (SImode, 2);
4525
4526           /* There is no "pascal" calling convention in 64bit ABI.  */
4527           if (TARGET_64BIT)
4528             abort ();
4529
4530           emit_insn (gen_popsi1 (ecx));
4531           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
4532           emit_jump_insn (gen_return_indirect_internal (ecx));
4533         }
4534       else
4535         emit_jump_insn (gen_return_pop_internal (popc));
4536     }
4537   else
4538     emit_jump_insn (gen_return_internal ());
4539 }
4540
4541 /* Reset from the function's potential modifications.  */
4542
4543 static void
4544 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
4545                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4546 {
4547   if (pic_offset_table_rtx)
4548     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
4549 }
4550 \f
4551 /* Extract the parts of an RTL expression that is a valid memory address
4552    for an instruction.  Return 0 if the structure of the address is
4553    grossly off.  Return -1 if the address contains ASHIFT, so it is not
4554    strictly valid, but still used for computing length of lea instruction.  */
4555
4556 int
4557 ix86_decompose_address (rtx addr, struct ix86_address *out)
4558 {
4559   rtx base = NULL_RTX;
4560   rtx index = NULL_RTX;
4561   rtx disp = NULL_RTX;
4562   HOST_WIDE_INT scale = 1;
4563   rtx scale_rtx = NULL_RTX;
4564   int retval = 1;
4565   enum ix86_address_seg seg = SEG_DEFAULT;
4566
4567   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
4568     base = addr;
4569   else if (GET_CODE (addr) == PLUS)
4570     {
4571       rtx addends[4], op;
4572       int n = 0, i;
4573
4574       op = addr;
4575       do
4576         {
4577           if (n >= 4)
4578             return 0;
4579           addends[n++] = XEXP (op, 1);
4580           op = XEXP (op, 0);
4581         }
4582       while (GET_CODE (op) == PLUS);
4583       if (n >= 4)
4584         return 0;
4585       addends[n] = op;
4586
4587       for (i = n; i >= 0; --i)
4588         {
4589           op = addends[i];
4590           switch (GET_CODE (op))
4591             {
4592             case MULT:
4593               if (index)
4594                 return 0;
4595               index = XEXP (op, 0);
4596               scale_rtx = XEXP (op, 1);
4597               break;
4598
4599             case UNSPEC:
4600               if (XINT (op, 1) == UNSPEC_TP
4601                   && TARGET_TLS_DIRECT_SEG_REFS
4602                   && seg == SEG_DEFAULT)
4603                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
4604               else
4605                 return 0;
4606               break;
4607
4608             case REG:
4609             case SUBREG:
4610               if (!base)
4611                 base = op;
4612               else if (!index)
4613                 index = op;
4614               else
4615                 return 0;
4616               break;
4617
4618             case CONST:
4619             case CONST_INT:
4620             case SYMBOL_REF:
4621             case LABEL_REF:
4622               if (disp)
4623                 return 0;
4624               disp = op;
4625               break;
4626
4627             default:
4628               return 0;
4629             }
4630         }
4631     }
4632   else if (GET_CODE (addr) == MULT)
4633     {
4634       index = XEXP (addr, 0);           /* index*scale */
4635       scale_rtx = XEXP (addr, 1);
4636     }
4637   else if (GET_CODE (addr) == ASHIFT)
4638     {
4639       rtx tmp;
4640
4641       /* We're called for lea too, which implements ashift on occasion.  */
4642       index = XEXP (addr, 0);
4643       tmp = XEXP (addr, 1);
4644       if (GET_CODE (tmp) != CONST_INT)
4645         return 0;
4646       scale = INTVAL (tmp);
4647       if ((unsigned HOST_WIDE_INT) scale > 3)
4648         return 0;
4649       scale = 1 << scale;
4650       retval = -1;
4651     }
4652   else
4653     disp = addr;                        /* displacement */
4654
4655   /* Extract the integral value of scale.  */
4656   if (scale_rtx)
4657     {
4658       if (GET_CODE (scale_rtx) != CONST_INT)
4659         return 0;
4660       scale = INTVAL (scale_rtx);
4661     }
4662
4663   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
4664   if (base && index && scale == 1
4665       && (index == arg_pointer_rtx
4666           || index == frame_pointer_rtx
4667           || (REG_P (index) && REGNO (index) == STACK_POINTER_REGNUM)))
4668     {
4669       rtx tmp = base;
4670       base = index;
4671       index = tmp;
4672     }
4673
4674   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
4675   if ((base == hard_frame_pointer_rtx
4676        || base == frame_pointer_rtx
4677        || base == arg_pointer_rtx) && !disp)
4678     disp = const0_rtx;
4679
4680   /* Special case: on K6, [%esi] makes the instruction vector decoded.
4681      Avoid this by transforming to [%esi+0].  */
4682   if (ix86_tune == PROCESSOR_K6 && !optimize_size
4683       && base && !index && !disp
4684       && REG_P (base)
4685       && REGNO_REG_CLASS (REGNO (base)) == SIREG)
4686     disp = const0_rtx;
4687
4688   /* Special case: encode reg+reg instead of reg*2.  */
4689   if (!base && index && scale && scale == 2)
4690     base = index, scale = 1;
4691
4692   /* Special case: scaling cannot be encoded without base or displacement.  */
4693   if (!base && !disp && index && scale != 1)
4694     disp = const0_rtx;
4695
4696   out->base = base;
4697   out->index = index;
4698   out->disp = disp;
4699   out->scale = scale;
4700   out->seg = seg;
4701
4702   return retval;
4703 }
4704 \f
4705 /* Return cost of the memory address x.
4706    For i386, it is better to use a complex address than let gcc copy
4707    the address into a reg and make a new pseudo.  But not if the address
4708    requires to two regs - that would mean more pseudos with longer
4709    lifetimes.  */
4710 static int
4711 ix86_address_cost (rtx x)
4712 {
4713   struct ix86_address parts;
4714   int cost = 1;
4715
4716   if (!ix86_decompose_address (x, &parts))
4717     abort ();
4718
4719   /* More complex memory references are better.  */
4720   if (parts.disp && parts.disp != const0_rtx)
4721     cost--;
4722   if (parts.seg != SEG_DEFAULT)
4723     cost--;
4724
4725   /* Attempt to minimize number of registers in the address.  */
4726   if ((parts.base
4727        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
4728       || (parts.index
4729           && (!REG_P (parts.index)
4730               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
4731     cost++;
4732
4733   if (parts.base
4734       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
4735       && parts.index
4736       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
4737       && parts.base != parts.index)
4738     cost++;
4739
4740   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
4741      since it's predecode logic can't detect the length of instructions
4742      and it degenerates to vector decoded.  Increase cost of such
4743      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
4744      to split such addresses or even refuse such addresses at all.
4745
4746      Following addressing modes are affected:
4747       [base+scale*index]
4748       [scale*index+disp]
4749       [base+index]
4750
4751      The first and last case  may be avoidable by explicitly coding the zero in
4752      memory address, but I don't have AMD-K6 machine handy to check this
4753      theory.  */
4754
4755   if (TARGET_K6
4756       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
4757           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
4758           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
4759     cost += 10;
4760
4761   return cost;
4762 }
4763 \f
4764 /* If X is a machine specific address (i.e. a symbol or label being
4765    referenced as a displacement from the GOT implemented using an
4766    UNSPEC), then return the base term.  Otherwise return X.  */
4767
4768 rtx
4769 ix86_find_base_term (rtx x)
4770 {
4771   rtx term;
4772
4773   if (TARGET_64BIT)
4774     {
4775       if (GET_CODE (x) != CONST)
4776         return x;
4777       term = XEXP (x, 0);
4778       if (GET_CODE (term) == PLUS
4779           && (GET_CODE (XEXP (term, 1)) == CONST_INT
4780               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
4781         term = XEXP (term, 0);
4782       if (GET_CODE (term) != UNSPEC
4783           || XINT (term, 1) != UNSPEC_GOTPCREL)
4784         return x;
4785
4786       term = XVECEXP (term, 0, 0);
4787
4788       if (GET_CODE (term) != SYMBOL_REF
4789           && GET_CODE (term) != LABEL_REF)
4790         return x;
4791
4792       return term;
4793     }
4794
4795   term = ix86_delegitimize_address (x);
4796
4797   if (GET_CODE (term) != SYMBOL_REF
4798       && GET_CODE (term) != LABEL_REF)
4799     return x;
4800
4801   return term;
4802 }
4803
4804 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
4805    this is used for to form addresses to local data when -fPIC is in
4806    use.  */
4807
4808 static bool
4809 darwin_local_data_pic (rtx disp)
4810 {
4811   if (GET_CODE (disp) == MINUS)
4812     {
4813       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
4814           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
4815         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
4816           {
4817             const char *sym_name = XSTR (XEXP (disp, 1), 0);
4818             if (! strcmp (sym_name, "<pic base>"))
4819               return true;
4820           }
4821     }
4822
4823   return false;
4824 }
4825 \f
4826 /* Determine if a given RTX is a valid constant.  We already know this
4827    satisfies CONSTANT_P.  */
4828
4829 bool
4830 legitimate_constant_p (rtx x)
4831 {
4832   switch (GET_CODE (x))
4833     {
4834     case CONST:
4835       x = XEXP (x, 0);
4836
4837       if (GET_CODE (x) == PLUS)
4838         {
4839           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4840             return false;
4841           x = XEXP (x, 0);
4842         }
4843
4844       if (TARGET_MACHO && darwin_local_data_pic (x))
4845         return true;
4846
4847       /* Only some unspecs are valid as "constants".  */
4848       if (GET_CODE (x) == UNSPEC)
4849         switch (XINT (x, 1))
4850           {
4851           case UNSPEC_TPOFF:
4852           case UNSPEC_NTPOFF:
4853             return local_exec_symbolic_operand (XVECEXP (x, 0, 0), Pmode);
4854           case UNSPEC_DTPOFF:
4855             return local_dynamic_symbolic_operand (XVECEXP (x, 0, 0), Pmode);
4856           default:
4857             return false;
4858           }
4859
4860       /* We must have drilled down to a symbol.  */
4861       if (!symbolic_operand (x, Pmode))
4862         return false;
4863       /* FALLTHRU */
4864
4865     case SYMBOL_REF:
4866       /* TLS symbols are never valid.  */
4867       if (tls_symbolic_operand (x, Pmode))
4868         return false;
4869       break;
4870
4871     default:
4872       break;
4873     }
4874
4875   /* Otherwise we handle everything else in the move patterns.  */
4876   return true;
4877 }
4878
4879 /* Determine if it's legal to put X into the constant pool.  This
4880    is not possible for the address of thread-local symbols, which
4881    is checked above.  */
4882
4883 static bool
4884 ix86_cannot_force_const_mem (rtx x)
4885 {
4886   return !legitimate_constant_p (x);
4887 }
4888
4889 /* Determine if a given RTX is a valid constant address.  */
4890
4891 bool
4892 constant_address_p (rtx x)
4893 {
4894   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
4895 }
4896
4897 /* Nonzero if the constant value X is a legitimate general operand
4898    when generating PIC code.  It is given that flag_pic is on and
4899    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
4900
4901 bool
4902 legitimate_pic_operand_p (rtx x)
4903 {
4904   rtx inner;
4905
4906   switch (GET_CODE (x))
4907     {
4908     case CONST:
4909       inner = XEXP (x, 0);
4910
4911       /* Only some unspecs are valid as "constants".  */
4912       if (GET_CODE (inner) == UNSPEC)
4913         switch (XINT (inner, 1))
4914           {
4915           case UNSPEC_TPOFF:
4916             return local_exec_symbolic_operand (XVECEXP (inner, 0, 0), Pmode);
4917           default:
4918             return false;
4919           }
4920       /* FALLTHRU */
4921
4922     case SYMBOL_REF:
4923     case LABEL_REF:
4924       return legitimate_pic_address_disp_p (x);
4925
4926     default:
4927       return true;
4928     }
4929 }
4930
4931 /* Determine if a given CONST RTX is a valid memory displacement
4932    in PIC mode.  */
4933
4934 int
4935 legitimate_pic_address_disp_p (rtx disp)
4936 {
4937   bool saw_plus;
4938
4939   /* In 64bit mode we can allow direct addresses of symbols and labels
4940      when they are not dynamic symbols.  */
4941   if (TARGET_64BIT)
4942     {
4943       /* TLS references should always be enclosed in UNSPEC.  */
4944       if (tls_symbolic_operand (disp, GET_MODE (disp)))
4945         return 0;
4946       if (GET_CODE (disp) == SYMBOL_REF
4947           && ix86_cmodel == CM_SMALL_PIC
4948           && SYMBOL_REF_LOCAL_P (disp))
4949         return 1;
4950       if (GET_CODE (disp) == LABEL_REF)
4951         return 1;
4952       if (GET_CODE (disp) == CONST
4953           && GET_CODE (XEXP (disp, 0)) == PLUS)
4954         {
4955           rtx op0 = XEXP (XEXP (disp, 0), 0);
4956           rtx op1 = XEXP (XEXP (disp, 0), 1);
4957
4958           /* TLS references should always be enclosed in UNSPEC.  */
4959           if (tls_symbolic_operand (op0, GET_MODE (op0)))
4960             return 0;
4961           if (((GET_CODE (op0) == SYMBOL_REF
4962                 && ix86_cmodel == CM_SMALL_PIC
4963                 && SYMBOL_REF_LOCAL_P (op0))
4964                || GET_CODE (op0) == LABEL_REF)
4965               && GET_CODE (op1) == CONST_INT
4966               && INTVAL (op1) < 16*1024*1024
4967               && INTVAL (op1) >= -16*1024*1024)
4968             return 1;
4969         }
4970     }
4971   if (GET_CODE (disp) != CONST)
4972     return 0;
4973   disp = XEXP (disp, 0);
4974
4975   if (TARGET_64BIT)
4976     {
4977       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
4978          of GOT tables.  We should not need these anyway.  */
4979       if (GET_CODE (disp) != UNSPEC
4980           || XINT (disp, 1) != UNSPEC_GOTPCREL)
4981         return 0;
4982
4983       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
4984           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
4985         return 0;
4986       return 1;
4987     }
4988
4989   saw_plus = false;
4990   if (GET_CODE (disp) == PLUS)
4991     {
4992       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
4993         return 0;
4994       disp = XEXP (disp, 0);
4995       saw_plus = true;
4996     }
4997
4998   if (TARGET_MACHO && darwin_local_data_pic (disp))
4999     return 1;
5000
5001   if (GET_CODE (disp) != UNSPEC)
5002     return 0;
5003
5004   switch (XINT (disp, 1))
5005     {
5006     case UNSPEC_GOT:
5007       if (saw_plus)
5008         return false;
5009       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
5010     case UNSPEC_GOTOFF:
5011       if (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
5012           || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
5013         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5014       return false;
5015     case UNSPEC_GOTTPOFF:
5016     case UNSPEC_GOTNTPOFF:
5017     case UNSPEC_INDNTPOFF:
5018       if (saw_plus)
5019         return false;
5020       return initial_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5021     case UNSPEC_NTPOFF:
5022       return local_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5023     case UNSPEC_DTPOFF:
5024       return local_dynamic_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5025     }
5026
5027   return 0;
5028 }
5029
5030 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
5031    memory address for an instruction.  The MODE argument is the machine mode
5032    for the MEM expression that wants to use this address.
5033
5034    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
5035    convert common non-canonical forms to canonical form so that they will
5036    be recognized.  */
5037
5038 int
5039 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
5040 {
5041   struct ix86_address parts;
5042   rtx base, index, disp;
5043   HOST_WIDE_INT scale;
5044   const char *reason = NULL;
5045   rtx reason_rtx = NULL_RTX;
5046
5047   if (TARGET_DEBUG_ADDR)
5048     {
5049       fprintf (stderr,
5050                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
5051                GET_MODE_NAME (mode), strict);
5052       debug_rtx (addr);
5053     }
5054
5055   if (ix86_decompose_address (addr, &parts) <= 0)
5056     {
5057       reason = "decomposition failed";
5058       goto report_error;
5059     }
5060
5061   base = parts.base;
5062   index = parts.index;
5063   disp = parts.disp;
5064   scale = parts.scale;
5065
5066   /* Validate base register.
5067
5068      Don't allow SUBREG's here, it can lead to spill failures when the base
5069      is one word out of a two word structure, which is represented internally
5070      as a DImode int.  */
5071
5072   if (base)
5073     {
5074       reason_rtx = base;
5075
5076       if (GET_CODE (base) != REG)
5077         {
5078           reason = "base is not a register";
5079           goto report_error;
5080         }
5081
5082       if (GET_MODE (base) != Pmode)
5083         {
5084           reason = "base is not in Pmode";
5085           goto report_error;
5086         }
5087
5088       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (base))
5089           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (base)))
5090         {
5091           reason = "base is not valid";
5092           goto report_error;
5093         }
5094     }
5095
5096   /* Validate index register.
5097
5098      Don't allow SUBREG's here, it can lead to spill failures when the index
5099      is one word out of a two word structure, which is represented internally
5100      as a DImode int.  */
5101
5102   if (index)
5103     {
5104       reason_rtx = index;
5105
5106       if (GET_CODE (index) != REG)
5107         {
5108           reason = "index is not a register";
5109           goto report_error;
5110         }
5111
5112       if (GET_MODE (index) != Pmode)
5113         {
5114           reason = "index is not in Pmode";
5115           goto report_error;
5116         }
5117
5118       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (index))
5119           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (index)))
5120         {
5121           reason = "index is not valid";
5122           goto report_error;
5123         }
5124     }
5125
5126   /* Validate scale factor.  */
5127   if (scale != 1)
5128     {
5129       reason_rtx = GEN_INT (scale);
5130       if (!index)
5131         {
5132           reason = "scale without index";
5133           goto report_error;
5134         }
5135
5136       if (scale != 2 && scale != 4 && scale != 8)
5137         {
5138           reason = "scale is not a valid multiplier";
5139           goto report_error;
5140         }
5141     }
5142
5143   /* Validate displacement.  */
5144   if (disp)
5145     {
5146       reason_rtx = disp;
5147
5148       if (GET_CODE (disp) == CONST
5149           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
5150         switch (XINT (XEXP (disp, 0), 1))
5151           {
5152           case UNSPEC_GOT:
5153           case UNSPEC_GOTOFF:
5154           case UNSPEC_GOTPCREL:
5155             if (!flag_pic)
5156               abort ();
5157             goto is_legitimate_pic;
5158
5159           case UNSPEC_GOTTPOFF:
5160           case UNSPEC_GOTNTPOFF:
5161           case UNSPEC_INDNTPOFF:
5162           case UNSPEC_NTPOFF:
5163           case UNSPEC_DTPOFF:
5164             break;
5165
5166           default:
5167             reason = "invalid address unspec";
5168             goto report_error;
5169           }
5170
5171       else if (flag_pic && (SYMBOLIC_CONST (disp)
5172 #if TARGET_MACHO
5173                             && !machopic_operand_p (disp)
5174 #endif
5175                             ))
5176         {
5177         is_legitimate_pic:
5178           if (TARGET_64BIT && (index || base))
5179             {
5180               /* foo@dtpoff(%rX) is ok.  */
5181               if (GET_CODE (disp) != CONST
5182                   || GET_CODE (XEXP (disp, 0)) != PLUS
5183                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
5184                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
5185                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
5186                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
5187                 {
5188                   reason = "non-constant pic memory reference";
5189                   goto report_error;
5190                 }
5191             }
5192           else if (! legitimate_pic_address_disp_p (disp))
5193             {
5194               reason = "displacement is an invalid pic construct";
5195               goto report_error;
5196             }
5197
5198           /* This code used to verify that a symbolic pic displacement
5199              includes the pic_offset_table_rtx register.
5200
5201              While this is good idea, unfortunately these constructs may
5202              be created by "adds using lea" optimization for incorrect
5203              code like:
5204
5205              int a;
5206              int foo(int i)
5207                {
5208                  return *(&a+i);
5209                }
5210
5211              This code is nonsensical, but results in addressing
5212              GOT table with pic_offset_table_rtx base.  We can't
5213              just refuse it easily, since it gets matched by
5214              "addsi3" pattern, that later gets split to lea in the
5215              case output register differs from input.  While this
5216              can be handled by separate addsi pattern for this case
5217              that never results in lea, this seems to be easier and
5218              correct fix for crash to disable this test.  */
5219         }
5220       else if (GET_CODE (disp) != LABEL_REF
5221                && GET_CODE (disp) != CONST_INT
5222                && (GET_CODE (disp) != CONST
5223                    || !legitimate_constant_p (disp))
5224                && (GET_CODE (disp) != SYMBOL_REF
5225                    || !legitimate_constant_p (disp)))
5226         {
5227           reason = "displacement is not constant";
5228           goto report_error;
5229         }
5230       else if (TARGET_64BIT
5231                && !x86_64_immediate_operand (disp, VOIDmode))
5232         {
5233           reason = "displacement is out of range";
5234           goto report_error;
5235         }
5236     }
5237
5238   /* Everything looks valid.  */
5239   if (TARGET_DEBUG_ADDR)
5240     fprintf (stderr, "Success.\n");
5241   return TRUE;
5242
5243  report_error:
5244   if (TARGET_DEBUG_ADDR)
5245     {
5246       fprintf (stderr, "Error: %s\n", reason);
5247       debug_rtx (reason_rtx);
5248     }
5249   return FALSE;
5250 }
5251 \f
5252 /* Return an unique alias set for the GOT.  */
5253
5254 static HOST_WIDE_INT
5255 ix86_GOT_alias_set (void)
5256 {
5257   static HOST_WIDE_INT set = -1;
5258   if (set == -1)
5259     set = new_alias_set ();
5260   return set;
5261 }
5262
5263 /* Return a legitimate reference for ORIG (an address) using the
5264    register REG.  If REG is 0, a new pseudo is generated.
5265
5266    There are two types of references that must be handled:
5267
5268    1. Global data references must load the address from the GOT, via
5269       the PIC reg.  An insn is emitted to do this load, and the reg is
5270       returned.
5271
5272    2. Static data references, constant pool addresses, and code labels
5273       compute the address as an offset from the GOT, whose base is in
5274       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
5275       differentiate them from global data objects.  The returned
5276       address is the PIC reg + an unspec constant.
5277
5278    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
5279    reg also appears in the address.  */
5280
5281 static rtx
5282 legitimize_pic_address (rtx orig, rtx reg)
5283 {
5284   rtx addr = orig;
5285   rtx new = orig;
5286   rtx base;
5287
5288 #if TARGET_MACHO
5289   if (reg == 0)
5290     reg = gen_reg_rtx (Pmode);
5291   /* Use the generic Mach-O PIC machinery.  */
5292   return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
5293 #endif
5294
5295   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
5296     new = addr;
5297   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
5298     {
5299       /* This symbol may be referenced via a displacement from the PIC
5300          base address (@GOTOFF).  */
5301
5302       if (reload_in_progress)
5303         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5304       if (GET_CODE (addr) == CONST)
5305         addr = XEXP (addr, 0);
5306       if (GET_CODE (addr) == PLUS)
5307           {
5308             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
5309             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
5310           }
5311         else
5312           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
5313       new = gen_rtx_CONST (Pmode, new);
5314       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5315
5316       if (reg != 0)
5317         {
5318           emit_move_insn (reg, new);
5319           new = reg;
5320         }
5321     }
5322   else if (GET_CODE (addr) == SYMBOL_REF)
5323     {
5324       if (TARGET_64BIT)
5325         {
5326           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
5327           new = gen_rtx_CONST (Pmode, new);
5328           new = gen_const_mem (Pmode, new);
5329           set_mem_alias_set (new, ix86_GOT_alias_set ());
5330
5331           if (reg == 0)
5332             reg = gen_reg_rtx (Pmode);
5333           /* Use directly gen_movsi, otherwise the address is loaded
5334              into register for CSE.  We don't want to CSE this addresses,
5335              instead we CSE addresses from the GOT table, so skip this.  */
5336           emit_insn (gen_movsi (reg, new));
5337           new = reg;
5338         }
5339       else
5340         {
5341           /* This symbol must be referenced via a load from the
5342              Global Offset Table (@GOT).  */
5343
5344           if (reload_in_progress)
5345             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5346           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
5347           new = gen_rtx_CONST (Pmode, new);
5348           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5349           new = gen_const_mem (Pmode, new);
5350           set_mem_alias_set (new, ix86_GOT_alias_set ());
5351
5352           if (reg == 0)
5353             reg = gen_reg_rtx (Pmode);
5354           emit_move_insn (reg, new);
5355           new = reg;
5356         }
5357     }
5358   else
5359     {
5360       if (GET_CODE (addr) == CONST)
5361         {
5362           addr = XEXP (addr, 0);
5363
5364           /* We must match stuff we generate before.  Assume the only
5365              unspecs that can get here are ours.  Not that we could do
5366              anything with them anyway....  */
5367           if (GET_CODE (addr) == UNSPEC
5368               || (GET_CODE (addr) == PLUS
5369                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
5370             return orig;
5371           if (GET_CODE (addr) != PLUS)
5372             abort ();
5373         }
5374       if (GET_CODE (addr) == PLUS)
5375         {
5376           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
5377
5378           /* Check first to see if this is a constant offset from a @GOTOFF
5379              symbol reference.  */
5380           if (local_symbolic_operand (op0, Pmode)
5381               && GET_CODE (op1) == CONST_INT)
5382             {
5383               if (!TARGET_64BIT)
5384                 {
5385                   if (reload_in_progress)
5386                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5387                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
5388                                         UNSPEC_GOTOFF);
5389                   new = gen_rtx_PLUS (Pmode, new, op1);
5390                   new = gen_rtx_CONST (Pmode, new);
5391                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5392
5393                   if (reg != 0)
5394                     {
5395                       emit_move_insn (reg, new);
5396                       new = reg;
5397                     }
5398                 }
5399               else
5400                 {
5401                   if (INTVAL (op1) < -16*1024*1024
5402                       || INTVAL (op1) >= 16*1024*1024)
5403                     new = gen_rtx_PLUS (Pmode, op0, force_reg (Pmode, op1));
5404                 }
5405             }
5406           else
5407             {
5408               base = legitimize_pic_address (XEXP (addr, 0), reg);
5409               new  = legitimize_pic_address (XEXP (addr, 1),
5410                                              base == reg ? NULL_RTX : reg);
5411
5412               if (GET_CODE (new) == CONST_INT)
5413                 new = plus_constant (base, INTVAL (new));
5414               else
5415                 {
5416                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
5417                     {
5418                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
5419                       new = XEXP (new, 1);
5420                     }
5421                   new = gen_rtx_PLUS (Pmode, base, new);
5422                 }
5423             }
5424         }
5425     }
5426   return new;
5427 }
5428 \f
5429 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
5430
5431 static rtx
5432 get_thread_pointer (int to_reg)
5433 {
5434   rtx tp, reg, insn;
5435
5436   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
5437   if (!to_reg)
5438     return tp;
5439
5440   reg = gen_reg_rtx (Pmode);
5441   insn = gen_rtx_SET (VOIDmode, reg, tp);
5442   insn = emit_insn (insn);
5443
5444   return reg;
5445 }
5446
5447 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
5448    false if we expect this to be used for a memory address and true if
5449    we expect to load the address into a register.  */
5450
5451 static rtx
5452 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
5453 {
5454   rtx dest, base, off, pic;
5455   int type;
5456
5457   switch (model)
5458     {
5459     case TLS_MODEL_GLOBAL_DYNAMIC:
5460       dest = gen_reg_rtx (Pmode);
5461       if (TARGET_64BIT)
5462         {
5463           rtx rax = gen_rtx_REG (Pmode, 0), insns;
5464
5465           start_sequence ();
5466           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
5467           insns = get_insns ();
5468           end_sequence ();
5469
5470           emit_libcall_block (insns, dest, rax, x);
5471         }
5472       else
5473         emit_insn (gen_tls_global_dynamic_32 (dest, x));
5474       break;
5475
5476     case TLS_MODEL_LOCAL_DYNAMIC:
5477       base = gen_reg_rtx (Pmode);
5478       if (TARGET_64BIT)
5479         {
5480           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
5481
5482           start_sequence ();
5483           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
5484           insns = get_insns ();
5485           end_sequence ();
5486
5487           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
5488           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
5489           emit_libcall_block (insns, base, rax, note);
5490         }
5491       else
5492         emit_insn (gen_tls_local_dynamic_base_32 (base));
5493
5494       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
5495       off = gen_rtx_CONST (Pmode, off);
5496
5497       return gen_rtx_PLUS (Pmode, base, off);
5498
5499     case TLS_MODEL_INITIAL_EXEC:
5500       if (TARGET_64BIT)
5501         {
5502           pic = NULL;
5503           type = UNSPEC_GOTNTPOFF;
5504         }
5505       else if (flag_pic)
5506         {
5507           if (reload_in_progress)
5508             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5509           pic = pic_offset_table_rtx;
5510           type = TARGET_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
5511         }
5512       else if (!TARGET_GNU_TLS)
5513         {
5514           pic = gen_reg_rtx (Pmode);
5515           emit_insn (gen_set_got (pic));
5516           type = UNSPEC_GOTTPOFF;
5517         }
5518       else
5519         {
5520           pic = NULL;
5521           type = UNSPEC_INDNTPOFF;
5522         }
5523
5524       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
5525       off = gen_rtx_CONST (Pmode, off);
5526       if (pic)
5527         off = gen_rtx_PLUS (Pmode, pic, off);
5528       off = gen_const_mem (Pmode, off);
5529       set_mem_alias_set (off, ix86_GOT_alias_set ());
5530
5531       if (TARGET_64BIT || TARGET_GNU_TLS)
5532         {
5533           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
5534           off = force_reg (Pmode, off);
5535           return gen_rtx_PLUS (Pmode, base, off);
5536         }
5537       else
5538         {
5539           base = get_thread_pointer (true);
5540           dest = gen_reg_rtx (Pmode);
5541           emit_insn (gen_subsi3 (dest, base, off));
5542         }
5543       break;
5544
5545     case TLS_MODEL_LOCAL_EXEC:
5546       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
5547                             (TARGET_64BIT || TARGET_GNU_TLS)
5548                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
5549       off = gen_rtx_CONST (Pmode, off);
5550
5551       if (TARGET_64BIT || TARGET_GNU_TLS)
5552         {
5553           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
5554           return gen_rtx_PLUS (Pmode, base, off);
5555         }
5556       else
5557         {
5558           base = get_thread_pointer (true);
5559           dest = gen_reg_rtx (Pmode);
5560           emit_insn (gen_subsi3 (dest, base, off));
5561         }
5562       break;
5563
5564     default:
5565       abort ();
5566     }
5567
5568   return dest;
5569 }
5570
5571 /* Try machine-dependent ways of modifying an illegitimate address
5572    to be legitimate.  If we find one, return the new, valid address.
5573    This macro is used in only one place: `memory_address' in explow.c.
5574
5575    OLDX is the address as it was before break_out_memory_refs was called.
5576    In some cases it is useful to look at this to decide what needs to be done.
5577
5578    MODE and WIN are passed so that this macro can use
5579    GO_IF_LEGITIMATE_ADDRESS.
5580
5581    It is always safe for this macro to do nothing.  It exists to recognize
5582    opportunities to optimize the output.
5583
5584    For the 80386, we handle X+REG by loading X into a register R and
5585    using R+REG.  R will go in a general reg and indexing will be used.
5586    However, if REG is a broken-out memory address or multiplication,
5587    nothing needs to be done because REG can certainly go in a general reg.
5588
5589    When -fpic is used, special handling is needed for symbolic references.
5590    See comments by legitimize_pic_address in i386.c for details.  */
5591
5592 rtx
5593 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
5594 {
5595   int changed = 0;
5596   unsigned log;
5597
5598   if (TARGET_DEBUG_ADDR)
5599     {
5600       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
5601                GET_MODE_NAME (mode));
5602       debug_rtx (x);
5603     }
5604
5605   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
5606   if (log)
5607     return legitimize_tls_address (x, log, false);
5608   if (GET_CODE (x) == CONST
5609       && GET_CODE (XEXP (x, 0)) == PLUS
5610       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
5611       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
5612     {
5613       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
5614       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
5615     }
5616
5617   if (flag_pic && SYMBOLIC_CONST (x))
5618     return legitimize_pic_address (x, 0);
5619
5620   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
5621   if (GET_CODE (x) == ASHIFT
5622       && GET_CODE (XEXP (x, 1)) == CONST_INT
5623       && (log = (unsigned) exact_log2 (INTVAL (XEXP (x, 1)))) < 4)
5624     {
5625       changed = 1;
5626       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
5627                         GEN_INT (1 << log));
5628     }
5629
5630   if (GET_CODE (x) == PLUS)
5631     {
5632       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
5633
5634       if (GET_CODE (XEXP (x, 0)) == ASHIFT
5635           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5636           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) < 4)
5637         {
5638           changed = 1;
5639           XEXP (x, 0) = gen_rtx_MULT (Pmode,
5640                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
5641                                       GEN_INT (1 << log));
5642         }
5643
5644       if (GET_CODE (XEXP (x, 1)) == ASHIFT
5645           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5646           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1)))) < 4)
5647         {
5648           changed = 1;
5649           XEXP (x, 1) = gen_rtx_MULT (Pmode,
5650                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
5651                                       GEN_INT (1 << log));
5652         }
5653
5654       /* Put multiply first if it isn't already.  */
5655       if (GET_CODE (XEXP (x, 1)) == MULT)
5656         {
5657           rtx tmp = XEXP (x, 0);
5658           XEXP (x, 0) = XEXP (x, 1);
5659           XEXP (x, 1) = tmp;
5660           changed = 1;
5661         }
5662
5663       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
5664          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
5665          created by virtual register instantiation, register elimination, and
5666          similar optimizations.  */
5667       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
5668         {
5669           changed = 1;
5670           x = gen_rtx_PLUS (Pmode,
5671                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
5672                                           XEXP (XEXP (x, 1), 0)),
5673                             XEXP (XEXP (x, 1), 1));
5674         }
5675
5676       /* Canonicalize
5677          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
5678          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
5679       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
5680                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5681                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
5682                && CONSTANT_P (XEXP (x, 1)))
5683         {
5684           rtx constant;
5685           rtx other = NULL_RTX;
5686
5687           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5688             {
5689               constant = XEXP (x, 1);
5690               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
5691             }
5692           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
5693             {
5694               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
5695               other = XEXP (x, 1);
5696             }
5697           else
5698             constant = 0;
5699
5700           if (constant)
5701             {
5702               changed = 1;
5703               x = gen_rtx_PLUS (Pmode,
5704                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
5705                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
5706                                 plus_constant (other, INTVAL (constant)));
5707             }
5708         }
5709
5710       if (changed && legitimate_address_p (mode, x, FALSE))
5711         return x;
5712
5713       if (GET_CODE (XEXP (x, 0)) == MULT)
5714         {
5715           changed = 1;
5716           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
5717         }
5718
5719       if (GET_CODE (XEXP (x, 1)) == MULT)
5720         {
5721           changed = 1;
5722           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
5723         }
5724
5725       if (changed
5726           && GET_CODE (XEXP (x, 1)) == REG
5727           && GET_CODE (XEXP (x, 0)) == REG)
5728         return x;
5729
5730       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
5731         {
5732           changed = 1;
5733           x = legitimize_pic_address (x, 0);
5734         }
5735
5736       if (changed && legitimate_address_p (mode, x, FALSE))
5737         return x;
5738
5739       if (GET_CODE (XEXP (x, 0)) == REG)
5740         {
5741           rtx temp = gen_reg_rtx (Pmode);
5742           rtx val  = force_operand (XEXP (x, 1), temp);
5743           if (val != temp)
5744             emit_move_insn (temp, val);
5745
5746           XEXP (x, 1) = temp;
5747           return x;
5748         }
5749
5750       else if (GET_CODE (XEXP (x, 1)) == REG)
5751         {
5752           rtx temp = gen_reg_rtx (Pmode);
5753           rtx val  = force_operand (XEXP (x, 0), temp);
5754           if (val != temp)
5755             emit_move_insn (temp, val);
5756
5757           XEXP (x, 0) = temp;
5758           return x;
5759         }
5760     }
5761
5762   return x;
5763 }
5764 \f
5765 /* Print an integer constant expression in assembler syntax.  Addition
5766    and subtraction are the only arithmetic that may appear in these
5767    expressions.  FILE is the stdio stream to write to, X is the rtx, and
5768    CODE is the operand print code from the output string.  */
5769
5770 static void
5771 output_pic_addr_const (FILE *file, rtx x, int code)
5772 {
5773   char buf[256];
5774
5775   switch (GET_CODE (x))
5776     {
5777     case PC:
5778       if (flag_pic)
5779         putc ('.', file);
5780       else
5781         abort ();
5782       break;
5783
5784     case SYMBOL_REF:
5785      /* Mark the decl as referenced so that cgraph will output the function.  */
5786      if (SYMBOL_REF_DECL (x))
5787        mark_decl_referenced (SYMBOL_REF_DECL (x));
5788
5789       assemble_name (file, XSTR (x, 0));
5790       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
5791         fputs ("@PLT", file);
5792       break;
5793
5794     case LABEL_REF:
5795       x = XEXP (x, 0);
5796       /* FALLTHRU */
5797     case CODE_LABEL:
5798       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
5799       assemble_name (asm_out_file, buf);
5800       break;
5801
5802     case CONST_INT:
5803       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
5804       break;
5805
5806     case CONST:
5807       /* This used to output parentheses around the expression,
5808          but that does not work on the 386 (either ATT or BSD assembler).  */
5809       output_pic_addr_const (file, XEXP (x, 0), code);
5810       break;
5811
5812     case CONST_DOUBLE:
5813       if (GET_MODE (x) == VOIDmode)
5814         {
5815           /* We can use %d if the number is <32 bits and positive.  */
5816           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
5817             fprintf (file, "0x%lx%08lx",
5818                      (unsigned long) CONST_DOUBLE_HIGH (x),
5819                      (unsigned long) CONST_DOUBLE_LOW (x));
5820           else
5821             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
5822         }
5823       else
5824         /* We can't handle floating point constants;
5825            PRINT_OPERAND must handle them.  */
5826         output_operand_lossage ("floating constant misused");
5827       break;
5828
5829     case PLUS:
5830       /* Some assemblers need integer constants to appear first.  */
5831       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5832         {
5833           output_pic_addr_const (file, XEXP (x, 0), code);
5834           putc ('+', file);
5835           output_pic_addr_const (file, XEXP (x, 1), code);
5836         }
5837       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5838         {
5839           output_pic_addr_const (file, XEXP (x, 1), code);
5840           putc ('+', file);
5841           output_pic_addr_const (file, XEXP (x, 0), code);
5842         }
5843       else
5844         abort ();
5845       break;
5846
5847     case MINUS:
5848       if (!TARGET_MACHO)
5849         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
5850       output_pic_addr_const (file, XEXP (x, 0), code);
5851       putc ('-', file);
5852       output_pic_addr_const (file, XEXP (x, 1), code);
5853       if (!TARGET_MACHO)
5854         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
5855       break;
5856
5857      case UNSPEC:
5858        if (XVECLEN (x, 0) != 1)
5859          abort ();
5860        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
5861        switch (XINT (x, 1))
5862         {
5863         case UNSPEC_GOT:
5864           fputs ("@GOT", file);
5865           break;
5866         case UNSPEC_GOTOFF:
5867           fputs ("@GOTOFF", file);
5868           break;
5869         case UNSPEC_GOTPCREL:
5870           fputs ("@GOTPCREL(%rip)", file);
5871           break;
5872         case UNSPEC_GOTTPOFF:
5873           /* FIXME: This might be @TPOFF in Sun ld too.  */
5874           fputs ("@GOTTPOFF", file);
5875           break;
5876         case UNSPEC_TPOFF:
5877           fputs ("@TPOFF", file);
5878           break;
5879         case UNSPEC_NTPOFF:
5880           if (TARGET_64BIT)
5881             fputs ("@TPOFF", file);
5882           else
5883             fputs ("@NTPOFF", file);
5884           break;
5885         case UNSPEC_DTPOFF:
5886           fputs ("@DTPOFF", file);
5887           break;
5888         case UNSPEC_GOTNTPOFF:
5889           if (TARGET_64BIT)
5890             fputs ("@GOTTPOFF(%rip)", file);
5891           else
5892             fputs ("@GOTNTPOFF", file);
5893           break;
5894         case UNSPEC_INDNTPOFF:
5895           fputs ("@INDNTPOFF", file);
5896           break;
5897         default:
5898           output_operand_lossage ("invalid UNSPEC as operand");
5899           break;
5900         }
5901        break;
5902
5903     default:
5904       output_operand_lossage ("invalid expression as operand");
5905     }
5906 }
5907
5908 /* This is called from dwarfout.c via ASM_OUTPUT_DWARF_ADDR_CONST.
5909    We need to handle our special PIC relocations.  */
5910
5911 void
5912 i386_dwarf_output_addr_const (FILE *file, rtx x)
5913 {
5914 #ifdef ASM_QUAD
5915   fprintf (file, "%s", TARGET_64BIT ? ASM_QUAD : ASM_LONG);
5916 #else
5917   if (TARGET_64BIT)
5918     abort ();
5919   fprintf (file, "%s", ASM_LONG);
5920 #endif
5921   if (flag_pic)
5922     output_pic_addr_const (file, x, '\0');
5923   else
5924     output_addr_const (file, x);
5925   fputc ('\n', file);
5926 }
5927
5928 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
5929    We need to emit DTP-relative relocations.  */
5930
5931 void
5932 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
5933 {
5934   fputs (ASM_LONG, file);
5935   output_addr_const (file, x);
5936   fputs ("@DTPOFF", file);
5937   switch (size)
5938     {
5939     case 4:
5940       break;
5941     case 8:
5942       fputs (", 0", file);
5943       break;
5944     default:
5945       abort ();
5946    }
5947 }
5948
5949 /* In the name of slightly smaller debug output, and to cater to
5950    general assembler losage, recognize PIC+GOTOFF and turn it back
5951    into a direct symbol reference.  */
5952
5953 static rtx
5954 ix86_delegitimize_address (rtx orig_x)
5955 {
5956   rtx x = orig_x, y;
5957
5958   if (GET_CODE (x) == MEM)
5959     x = XEXP (x, 0);
5960
5961   if (TARGET_64BIT)
5962     {
5963       if (GET_CODE (x) != CONST
5964           || GET_CODE (XEXP (x, 0)) != UNSPEC
5965           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
5966           || GET_CODE (orig_x) != MEM)
5967         return orig_x;
5968       return XVECEXP (XEXP (x, 0), 0, 0);
5969     }
5970
5971   if (GET_CODE (x) != PLUS
5972       || GET_CODE (XEXP (x, 1)) != CONST)
5973     return orig_x;
5974
5975   if (GET_CODE (XEXP (x, 0)) == REG
5976       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
5977     /* %ebx + GOT/GOTOFF */
5978     y = NULL;
5979   else if (GET_CODE (XEXP (x, 0)) == PLUS)
5980     {
5981       /* %ebx + %reg * scale + GOT/GOTOFF */
5982       y = XEXP (x, 0);
5983       if (GET_CODE (XEXP (y, 0)) == REG
5984           && REGNO (XEXP (y, 0)) == PIC_OFFSET_TABLE_REGNUM)
5985         y = XEXP (y, 1);
5986       else if (GET_CODE (XEXP (y, 1)) == REG
5987                && REGNO (XEXP (y, 1)) == PIC_OFFSET_TABLE_REGNUM)
5988         y = XEXP (y, 0);
5989       else
5990         return orig_x;
5991       if (GET_CODE (y) != REG
5992           && GET_CODE (y) != MULT
5993           && GET_CODE (y) != ASHIFT)
5994         return orig_x;
5995     }
5996   else
5997     return orig_x;
5998
5999   x = XEXP (XEXP (x, 1), 0);
6000   if (GET_CODE (x) == UNSPEC
6001       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6002           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
6003     {
6004       if (y)
6005         return gen_rtx_PLUS (Pmode, y, XVECEXP (x, 0, 0));
6006       return XVECEXP (x, 0, 0);
6007     }
6008
6009   if (GET_CODE (x) == PLUS
6010       && GET_CODE (XEXP (x, 0)) == UNSPEC
6011       && GET_CODE (XEXP (x, 1)) == CONST_INT
6012       && ((XINT (XEXP (x, 0), 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6013           || (XINT (XEXP (x, 0), 1) == UNSPEC_GOTOFF
6014               && GET_CODE (orig_x) != MEM)))
6015     {
6016       x = gen_rtx_PLUS (VOIDmode, XVECEXP (XEXP (x, 0), 0, 0), XEXP (x, 1));
6017       if (y)
6018         return gen_rtx_PLUS (Pmode, y, x);
6019       return x;
6020     }
6021
6022   return orig_x;
6023 }
6024 \f
6025 static void
6026 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
6027                     int fp, FILE *file)
6028 {
6029   const char *suffix;
6030
6031   if (mode == CCFPmode || mode == CCFPUmode)
6032     {
6033       enum rtx_code second_code, bypass_code;
6034       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
6035       if (bypass_code != UNKNOWN || second_code != UNKNOWN)
6036         abort ();
6037       code = ix86_fp_compare_code_to_integer (code);
6038       mode = CCmode;
6039     }
6040   if (reverse)
6041     code = reverse_condition (code);
6042
6043   switch (code)
6044     {
6045     case EQ:
6046       suffix = "e";
6047       break;
6048     case NE:
6049       suffix = "ne";
6050       break;
6051     case GT:
6052       if (mode != CCmode && mode != CCNOmode && mode != CCGCmode)
6053         abort ();
6054       suffix = "g";
6055       break;
6056     case GTU:
6057       /* ??? Use "nbe" instead of "a" for fcmov losage on some assemblers.
6058          Those same assemblers have the same but opposite losage on cmov.  */
6059       if (mode != CCmode)
6060         abort ();
6061       suffix = fp ? "nbe" : "a";
6062       break;
6063     case LT:
6064       if (mode == CCNOmode || mode == CCGOCmode)
6065         suffix = "s";
6066       else if (mode == CCmode || mode == CCGCmode)
6067         suffix = "l";
6068       else
6069         abort ();
6070       break;
6071     case LTU:
6072       if (mode != CCmode)
6073         abort ();
6074       suffix = "b";
6075       break;
6076     case GE:
6077       if (mode == CCNOmode || mode == CCGOCmode)
6078         suffix = "ns";
6079       else if (mode == CCmode || mode == CCGCmode)
6080         suffix = "ge";
6081       else
6082         abort ();
6083       break;
6084     case GEU:
6085       /* ??? As above.  */
6086       if (mode != CCmode)
6087         abort ();
6088       suffix = fp ? "nb" : "ae";
6089       break;
6090     case LE:
6091       if (mode != CCmode && mode != CCGCmode && mode != CCNOmode)
6092         abort ();
6093       suffix = "le";
6094       break;
6095     case LEU:
6096       if (mode != CCmode)
6097         abort ();
6098       suffix = "be";
6099       break;
6100     case UNORDERED:
6101       suffix = fp ? "u" : "p";
6102       break;
6103     case ORDERED:
6104       suffix = fp ? "nu" : "np";
6105       break;
6106     default:
6107       abort ();
6108     }
6109   fputs (suffix, file);
6110 }
6111
6112 /* Print the name of register X to FILE based on its machine mode and number.
6113    If CODE is 'w', pretend the mode is HImode.
6114    If CODE is 'b', pretend the mode is QImode.
6115    If CODE is 'k', pretend the mode is SImode.
6116    If CODE is 'q', pretend the mode is DImode.
6117    If CODE is 'h', pretend the reg is the `high' byte register.
6118    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
6119
6120 void
6121 print_reg (rtx x, int code, FILE *file)
6122 {
6123   if (REGNO (x) == ARG_POINTER_REGNUM
6124       || REGNO (x) == FRAME_POINTER_REGNUM
6125       || REGNO (x) == FLAGS_REG
6126       || REGNO (x) == FPSR_REG)
6127     abort ();
6128
6129   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
6130     putc ('%', file);
6131
6132   if (code == 'w' || MMX_REG_P (x))
6133     code = 2;
6134   else if (code == 'b')
6135     code = 1;
6136   else if (code == 'k')
6137     code = 4;
6138   else if (code == 'q')
6139     code = 8;
6140   else if (code == 'y')
6141     code = 3;
6142   else if (code == 'h')
6143     code = 0;
6144   else
6145     code = GET_MODE_SIZE (GET_MODE (x));
6146
6147   /* Irritatingly, AMD extended registers use different naming convention
6148      from the normal registers.  */
6149   if (REX_INT_REG_P (x))
6150     {
6151       if (!TARGET_64BIT)
6152         abort ();
6153       switch (code)
6154         {
6155           case 0:
6156             error ("extended registers have no high halves");
6157             break;
6158           case 1:
6159             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
6160             break;
6161           case 2:
6162             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
6163             break;
6164           case 4:
6165             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
6166             break;
6167           case 8:
6168             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
6169             break;
6170           default:
6171             error ("unsupported operand size for extended register");
6172             break;
6173         }
6174       return;
6175     }
6176   switch (code)
6177     {
6178     case 3:
6179       if (STACK_TOP_P (x))
6180         {
6181           fputs ("st(0)", file);
6182           break;
6183         }
6184       /* FALLTHRU */
6185     case 8:
6186     case 4:
6187     case 12:
6188       if (! ANY_FP_REG_P (x))
6189         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
6190       /* FALLTHRU */
6191     case 16:
6192     case 2:
6193     normal:
6194       fputs (hi_reg_name[REGNO (x)], file);
6195       break;
6196     case 1:
6197       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
6198         goto normal;
6199       fputs (qi_reg_name[REGNO (x)], file);
6200       break;
6201     case 0:
6202       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
6203         goto normal;
6204       fputs (qi_high_reg_name[REGNO (x)], file);
6205       break;
6206     default:
6207       abort ();
6208     }
6209 }
6210
6211 /* Locate some local-dynamic symbol still in use by this function
6212    so that we can print its name in some tls_local_dynamic_base
6213    pattern.  */
6214
6215 static const char *
6216 get_some_local_dynamic_name (void)
6217 {
6218   rtx insn;
6219
6220   if (cfun->machine->some_ld_name)
6221     return cfun->machine->some_ld_name;
6222
6223   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
6224     if (INSN_P (insn)
6225         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
6226       return cfun->machine->some_ld_name;
6227
6228   abort ();
6229 }
6230
6231 static int
6232 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
6233 {
6234   rtx x = *px;
6235
6236   if (GET_CODE (x) == SYMBOL_REF
6237       && local_dynamic_symbolic_operand (x, Pmode))
6238     {
6239       cfun->machine->some_ld_name = XSTR (x, 0);
6240       return 1;
6241     }
6242
6243   return 0;
6244 }
6245
6246 /* Meaning of CODE:
6247    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
6248    C -- print opcode suffix for set/cmov insn.
6249    c -- like C, but print reversed condition
6250    F,f -- likewise, but for floating-point.
6251    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
6252         otherwise nothing
6253    R -- print the prefix for register names.
6254    z -- print the opcode suffix for the size of the current operand.
6255    * -- print a star (in certain assembler syntax)
6256    A -- print an absolute memory reference.
6257    w -- print the operand as if it's a "word" (HImode) even if it isn't.
6258    s -- print a shift double count, followed by the assemblers argument
6259         delimiter.
6260    b -- print the QImode name of the register for the indicated operand.
6261         %b0 would print %al if operands[0] is reg 0.
6262    w --  likewise, print the HImode name of the register.
6263    k --  likewise, print the SImode name of the register.
6264    q --  likewise, print the DImode name of the register.
6265    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
6266    y -- print "st(0)" instead of "st" as a register.
6267    D -- print condition for SSE cmp instruction.
6268    P -- if PIC, print an @PLT suffix.
6269    X -- don't print any sort of PIC '@' suffix for a symbol.
6270    & -- print some in-use local-dynamic symbol name.
6271  */
6272
6273 void
6274 print_operand (FILE *file, rtx x, int code)
6275 {
6276   if (code)
6277     {
6278       switch (code)
6279         {
6280         case '*':
6281           if (ASSEMBLER_DIALECT == ASM_ATT)
6282             putc ('*', file);
6283           return;
6284
6285         case '&':
6286           assemble_name (file, get_some_local_dynamic_name ());
6287           return;
6288
6289         case 'A':
6290           if (ASSEMBLER_DIALECT == ASM_ATT)
6291             putc ('*', file);
6292           else if (ASSEMBLER_DIALECT == ASM_INTEL)
6293             {
6294               /* Intel syntax. For absolute addresses, registers should not
6295                  be surrounded by braces.  */
6296               if (GET_CODE (x) != REG)
6297                 {
6298                   putc ('[', file);
6299                   PRINT_OPERAND (file, x, 0);
6300                   putc (']', file);
6301                   return;
6302                 }
6303             }
6304           else
6305             abort ();
6306
6307           PRINT_OPERAND (file, x, 0);
6308           return;
6309
6310
6311         case 'L':
6312           if (ASSEMBLER_DIALECT == ASM_ATT)
6313             putc ('l', file);
6314           return;
6315
6316         case 'W':
6317           if (ASSEMBLER_DIALECT == ASM_ATT)
6318             putc ('w', file);
6319           return;
6320
6321         case 'B':
6322           if (ASSEMBLER_DIALECT == ASM_ATT)
6323             putc ('b', file);
6324           return;
6325
6326         case 'Q':
6327           if (ASSEMBLER_DIALECT == ASM_ATT)
6328             putc ('l', file);
6329           return;
6330
6331         case 'S':
6332           if (ASSEMBLER_DIALECT == ASM_ATT)
6333             putc ('s', file);
6334           return;
6335
6336         case 'T':
6337           if (ASSEMBLER_DIALECT == ASM_ATT)
6338             putc ('t', file);
6339           return;
6340
6341         case 'z':
6342           /* 387 opcodes don't get size suffixes if the operands are
6343              registers.  */
6344           if (STACK_REG_P (x))
6345             return;
6346
6347           /* Likewise if using Intel opcodes.  */
6348           if (ASSEMBLER_DIALECT == ASM_INTEL)
6349             return;
6350
6351           /* This is the size of op from size of operand.  */
6352           switch (GET_MODE_SIZE (GET_MODE (x)))
6353             {
6354             case 2:
6355 #ifdef HAVE_GAS_FILDS_FISTS
6356               putc ('s', file);
6357 #endif
6358               return;
6359
6360             case 4:
6361               if (GET_MODE (x) == SFmode)
6362                 {
6363                   putc ('s', file);
6364                   return;
6365                 }
6366               else
6367                 putc ('l', file);
6368               return;
6369
6370             case 12:
6371             case 16:
6372               putc ('t', file);
6373               return;
6374
6375             case 8:
6376               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
6377                 {
6378 #ifdef GAS_MNEMONICS
6379                   putc ('q', file);
6380 #else
6381                   putc ('l', file);
6382                   putc ('l', file);
6383 #endif
6384                 }
6385               else
6386                 putc ('l', file);
6387               return;
6388
6389             default:
6390               abort ();
6391             }
6392
6393         case 'b':
6394         case 'w':
6395         case 'k':
6396         case 'q':
6397         case 'h':
6398         case 'y':
6399         case 'X':
6400         case 'P':
6401           break;
6402
6403         case 's':
6404           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
6405             {
6406               PRINT_OPERAND (file, x, 0);
6407               putc (',', file);
6408             }
6409           return;
6410
6411         case 'D':
6412           /* Little bit of braindamage here.  The SSE compare instructions
6413              does use completely different names for the comparisons that the
6414              fp conditional moves.  */
6415           switch (GET_CODE (x))
6416             {
6417             case EQ:
6418             case UNEQ:
6419               fputs ("eq", file);
6420               break;
6421             case LT:
6422             case UNLT:
6423               fputs ("lt", file);
6424               break;
6425             case LE:
6426             case UNLE:
6427               fputs ("le", file);
6428               break;
6429             case UNORDERED:
6430               fputs ("unord", file);
6431               break;
6432             case NE:
6433             case LTGT:
6434               fputs ("neq", file);
6435               break;
6436             case UNGE:
6437             case GE:
6438               fputs ("nlt", file);
6439               break;
6440             case UNGT:
6441             case GT:
6442               fputs ("nle", file);
6443               break;
6444             case ORDERED:
6445               fputs ("ord", file);
6446               break;
6447             default:
6448               abort ();
6449               break;
6450             }
6451           return;
6452         case 'O':
6453 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
6454           if (ASSEMBLER_DIALECT == ASM_ATT)
6455             {
6456               switch (GET_MODE (x))
6457                 {
6458                 case HImode: putc ('w', file); break;
6459                 case SImode:
6460                 case SFmode: putc ('l', file); break;
6461                 case DImode:
6462                 case DFmode: putc ('q', file); break;
6463                 default: abort ();
6464                 }
6465               putc ('.', file);
6466             }
6467 #endif
6468           return;
6469         case 'C':
6470           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
6471           return;
6472         case 'F':
6473 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
6474           if (ASSEMBLER_DIALECT == ASM_ATT)
6475             putc ('.', file);
6476 #endif
6477           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
6478           return;
6479
6480           /* Like above, but reverse condition */
6481         case 'c':
6482           /* Check to see if argument to %c is really a constant
6483              and not a condition code which needs to be reversed.  */
6484           if (!COMPARISON_P (x))
6485           {
6486             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
6487              return;
6488           }
6489           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
6490           return;
6491         case 'f':
6492 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
6493           if (ASSEMBLER_DIALECT == ASM_ATT)
6494             putc ('.', file);
6495 #endif
6496           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
6497           return;
6498         case '+':
6499           {
6500             rtx x;
6501
6502             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
6503               return;
6504
6505             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
6506             if (x)
6507               {
6508                 int pred_val = INTVAL (XEXP (x, 0));
6509
6510                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
6511                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
6512                   {
6513                     int taken = pred_val > REG_BR_PROB_BASE / 2;
6514                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
6515
6516                     /* Emit hints only in the case default branch prediction
6517                        heuristics would fail.  */
6518                     if (taken != cputaken)
6519                       {
6520                         /* We use 3e (DS) prefix for taken branches and
6521                            2e (CS) prefix for not taken branches.  */
6522                         if (taken)
6523                           fputs ("ds ; ", file);
6524                         else
6525                           fputs ("cs ; ", file);
6526                       }
6527                   }
6528               }
6529             return;
6530           }
6531         default:
6532             output_operand_lossage ("invalid operand code `%c'", code);
6533         }
6534     }
6535
6536   if (GET_CODE (x) == REG)
6537     print_reg (x, code, file);
6538
6539   else if (GET_CODE (x) == MEM)
6540     {
6541       /* No `byte ptr' prefix for call instructions.  */
6542       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
6543         {
6544           const char * size;
6545           switch (GET_MODE_SIZE (GET_MODE (x)))
6546             {
6547             case 1: size = "BYTE"; break;
6548             case 2: size = "WORD"; break;
6549             case 4: size = "DWORD"; break;
6550             case 8: size = "QWORD"; break;
6551             case 12: size = "XWORD"; break;
6552             case 16: size = "XMMWORD"; break;
6553             default:
6554               abort ();
6555             }
6556
6557           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
6558           if (code == 'b')
6559             size = "BYTE";
6560           else if (code == 'w')
6561             size = "WORD";
6562           else if (code == 'k')
6563             size = "DWORD";
6564
6565           fputs (size, file);
6566           fputs (" PTR ", file);
6567         }
6568
6569       x = XEXP (x, 0);
6570       /* Avoid (%rip) for call operands.  */
6571       if (CONSTANT_ADDRESS_P (x) && code == 'P'
6572                && GET_CODE (x) != CONST_INT)
6573         output_addr_const (file, x);
6574       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
6575         output_operand_lossage ("invalid constraints for operand");
6576       else
6577         output_address (x);
6578     }
6579
6580   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
6581     {
6582       REAL_VALUE_TYPE r;
6583       long l;
6584
6585       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6586       REAL_VALUE_TO_TARGET_SINGLE (r, l);
6587
6588       if (ASSEMBLER_DIALECT == ASM_ATT)
6589         putc ('$', file);
6590       fprintf (file, "0x%08lx", l);
6591     }
6592
6593   /* These float cases don't actually occur as immediate operands.  */
6594   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
6595     {
6596       char dstr[30];
6597
6598       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
6599       fprintf (file, "%s", dstr);
6600     }
6601
6602   else if (GET_CODE (x) == CONST_DOUBLE
6603            && GET_MODE (x) == XFmode)
6604     {
6605       char dstr[30];
6606
6607       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
6608       fprintf (file, "%s", dstr);
6609     }
6610
6611   else
6612     {
6613       if (code != 'P')
6614         {
6615           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
6616             {
6617               if (ASSEMBLER_DIALECT == ASM_ATT)
6618                 putc ('$', file);
6619             }
6620           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
6621                    || GET_CODE (x) == LABEL_REF)
6622             {
6623               if (ASSEMBLER_DIALECT == ASM_ATT)
6624                 putc ('$', file);
6625               else
6626                 fputs ("OFFSET FLAT:", file);
6627             }
6628         }
6629       if (GET_CODE (x) == CONST_INT)
6630         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
6631       else if (flag_pic)
6632         output_pic_addr_const (file, x, code);
6633       else
6634         output_addr_const (file, x);
6635     }
6636 }
6637 \f
6638 /* Print a memory operand whose address is ADDR.  */
6639
6640 void
6641 print_operand_address (FILE *file, rtx addr)
6642 {
6643   struct ix86_address parts;
6644   rtx base, index, disp;
6645   int scale;
6646
6647   if (! ix86_decompose_address (addr, &parts))
6648     abort ();
6649
6650   base = parts.base;
6651   index = parts.index;
6652   disp = parts.disp;
6653   scale = parts.scale;
6654
6655   switch (parts.seg)
6656     {
6657     case SEG_DEFAULT:
6658       break;
6659     case SEG_FS:
6660     case SEG_GS:
6661       if (USER_LABEL_PREFIX[0] == 0)
6662         putc ('%', file);
6663       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
6664       break;
6665     default:
6666       abort ();
6667     }
6668
6669   if (!base && !index)
6670     {
6671       /* Displacement only requires special attention.  */
6672
6673       if (GET_CODE (disp) == CONST_INT)
6674         {
6675           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
6676             {
6677               if (USER_LABEL_PREFIX[0] == 0)
6678                 putc ('%', file);
6679               fputs ("ds:", file);
6680             }
6681           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
6682         }
6683       else if (flag_pic)
6684         output_pic_addr_const (file, disp, 0);
6685       else
6686         output_addr_const (file, disp);
6687
6688       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
6689       if (TARGET_64BIT
6690           && ((GET_CODE (disp) == SYMBOL_REF
6691                && ! tls_symbolic_operand (disp, GET_MODE (disp)))
6692               || GET_CODE (disp) == LABEL_REF
6693               || (GET_CODE (disp) == CONST
6694                   && GET_CODE (XEXP (disp, 0)) == PLUS
6695                   && (GET_CODE (XEXP (XEXP (disp, 0), 0)) == SYMBOL_REF
6696                       || GET_CODE (XEXP (XEXP (disp, 0), 0)) == LABEL_REF)
6697                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)))
6698         fputs ("(%rip)", file);
6699     }
6700   else
6701     {
6702       if (ASSEMBLER_DIALECT == ASM_ATT)
6703         {
6704           if (disp)
6705             {
6706               if (flag_pic)
6707                 output_pic_addr_const (file, disp, 0);
6708               else if (GET_CODE (disp) == LABEL_REF)
6709                 output_asm_label (disp);
6710               else
6711                 output_addr_const (file, disp);
6712             }
6713
6714           putc ('(', file);
6715           if (base)
6716             print_reg (base, 0, file);
6717           if (index)
6718             {
6719               putc (',', file);
6720               print_reg (index, 0, file);
6721               if (scale != 1)
6722                 fprintf (file, ",%d", scale);
6723             }
6724           putc (')', file);
6725         }
6726       else
6727         {
6728           rtx offset = NULL_RTX;
6729
6730           if (disp)
6731             {
6732               /* Pull out the offset of a symbol; print any symbol itself.  */
6733               if (GET_CODE (disp) == CONST
6734                   && GET_CODE (XEXP (disp, 0)) == PLUS
6735                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
6736                 {
6737                   offset = XEXP (XEXP (disp, 0), 1);
6738                   disp = gen_rtx_CONST (VOIDmode,
6739                                         XEXP (XEXP (disp, 0), 0));
6740                 }
6741
6742               if (flag_pic)
6743                 output_pic_addr_const (file, disp, 0);
6744               else if (GET_CODE (disp) == LABEL_REF)
6745                 output_asm_label (disp);
6746               else if (GET_CODE (disp) == CONST_INT)
6747                 offset = disp;
6748               else
6749                 output_addr_const (file, disp);
6750             }
6751
6752           putc ('[', file);
6753           if (base)
6754             {
6755               print_reg (base, 0, file);
6756               if (offset)
6757                 {
6758                   if (INTVAL (offset) >= 0)
6759                     putc ('+', file);
6760                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
6761                 }
6762             }
6763           else if (offset)
6764             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
6765           else
6766             putc ('0', file);
6767
6768           if (index)
6769             {
6770               putc ('+', file);
6771               print_reg (index, 0, file);
6772               if (scale != 1)
6773                 fprintf (file, "*%d", scale);
6774             }
6775           putc (']', file);
6776         }
6777     }
6778 }
6779
6780 bool
6781 output_addr_const_extra (FILE *file, rtx x)
6782 {
6783   rtx op;
6784
6785   if (GET_CODE (x) != UNSPEC)
6786     return false;
6787
6788   op = XVECEXP (x, 0, 0);
6789   switch (XINT (x, 1))
6790     {
6791     case UNSPEC_GOTTPOFF:
6792       output_addr_const (file, op);
6793       /* FIXME: This might be @TPOFF in Sun ld.  */
6794       fputs ("@GOTTPOFF", file);
6795       break;
6796     case UNSPEC_TPOFF:
6797       output_addr_const (file, op);
6798       fputs ("@TPOFF", file);
6799       break;
6800     case UNSPEC_NTPOFF:
6801       output_addr_const (file, op);
6802       if (TARGET_64BIT)
6803         fputs ("@TPOFF", file);
6804       else
6805         fputs ("@NTPOFF", file);
6806       break;
6807     case UNSPEC_DTPOFF:
6808       output_addr_const (file, op);
6809       fputs ("@DTPOFF", file);
6810       break;
6811     case UNSPEC_GOTNTPOFF:
6812       output_addr_const (file, op);
6813       if (TARGET_64BIT)
6814         fputs ("@GOTTPOFF(%rip)", file);
6815       else
6816         fputs ("@GOTNTPOFF", file);
6817       break;
6818     case UNSPEC_INDNTPOFF:
6819       output_addr_const (file, op);
6820       fputs ("@INDNTPOFF", file);
6821       break;
6822
6823     default:
6824       return false;
6825     }
6826
6827   return true;
6828 }
6829 \f
6830 /* Split one or more DImode RTL references into pairs of SImode
6831    references.  The RTL can be REG, offsettable MEM, integer constant, or
6832    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
6833    split and "num" is its length.  lo_half and hi_half are output arrays
6834    that parallel "operands".  */
6835
6836 void
6837 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
6838 {
6839   while (num--)
6840     {
6841       rtx op = operands[num];
6842
6843       /* simplify_subreg refuse to split volatile memory addresses,
6844          but we still have to handle it.  */
6845       if (GET_CODE (op) == MEM)
6846         {
6847           lo_half[num] = adjust_address (op, SImode, 0);
6848           hi_half[num] = adjust_address (op, SImode, 4);
6849         }
6850       else
6851         {
6852           lo_half[num] = simplify_gen_subreg (SImode, op,
6853                                               GET_MODE (op) == VOIDmode
6854                                               ? DImode : GET_MODE (op), 0);
6855           hi_half[num] = simplify_gen_subreg (SImode, op,
6856                                               GET_MODE (op) == VOIDmode
6857                                               ? DImode : GET_MODE (op), 4);
6858         }
6859     }
6860 }
6861 /* Split one or more TImode RTL references into pairs of SImode
6862    references.  The RTL can be REG, offsettable MEM, integer constant, or
6863    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
6864    split and "num" is its length.  lo_half and hi_half are output arrays
6865    that parallel "operands".  */
6866
6867 void
6868 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
6869 {
6870   while (num--)
6871     {
6872       rtx op = operands[num];
6873
6874       /* simplify_subreg refuse to split volatile memory addresses, but we
6875          still have to handle it.  */
6876       if (GET_CODE (op) == MEM)
6877         {
6878           lo_half[num] = adjust_address (op, DImode, 0);
6879           hi_half[num] = adjust_address (op, DImode, 8);
6880         }
6881       else
6882         {
6883           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
6884           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
6885         }
6886     }
6887 }
6888 \f
6889 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
6890    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
6891    is the expression of the binary operation.  The output may either be
6892    emitted here, or returned to the caller, like all output_* functions.
6893
6894    There is no guarantee that the operands are the same mode, as they
6895    might be within FLOAT or FLOAT_EXTEND expressions.  */
6896
6897 #ifndef SYSV386_COMPAT
6898 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
6899    wants to fix the assemblers because that causes incompatibility
6900    with gcc.  No-one wants to fix gcc because that causes
6901    incompatibility with assemblers...  You can use the option of
6902    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
6903 #define SYSV386_COMPAT 1
6904 #endif
6905
6906 const char *
6907 output_387_binary_op (rtx insn, rtx *operands)
6908 {
6909   static char buf[30];
6910   const char *p;
6911   const char *ssep;
6912   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]) | SSE_REG_P (operands[2]);
6913
6914 #ifdef ENABLE_CHECKING
6915   /* Even if we do not want to check the inputs, this documents input
6916      constraints.  Which helps in understanding the following code.  */
6917   if (STACK_REG_P (operands[0])
6918       && ((REG_P (operands[1])
6919            && REGNO (operands[0]) == REGNO (operands[1])
6920            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
6921           || (REG_P (operands[2])
6922               && REGNO (operands[0]) == REGNO (operands[2])
6923               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
6924       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
6925     ; /* ok */
6926   else if (!is_sse)
6927     abort ();
6928 #endif
6929
6930   switch (GET_CODE (operands[3]))
6931     {
6932     case PLUS:
6933       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6934           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6935         p = "fiadd";
6936       else
6937         p = "fadd";
6938       ssep = "add";
6939       break;
6940
6941     case MINUS:
6942       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6943           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6944         p = "fisub";
6945       else
6946         p = "fsub";
6947       ssep = "sub";
6948       break;
6949
6950     case MULT:
6951       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6952           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6953         p = "fimul";
6954       else
6955         p = "fmul";
6956       ssep = "mul";
6957       break;
6958
6959     case DIV:
6960       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6961           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6962         p = "fidiv";
6963       else
6964         p = "fdiv";
6965       ssep = "div";
6966       break;
6967
6968     default:
6969       abort ();
6970     }
6971
6972   if (is_sse)
6973    {
6974       strcpy (buf, ssep);
6975       if (GET_MODE (operands[0]) == SFmode)
6976         strcat (buf, "ss\t{%2, %0|%0, %2}");
6977       else
6978         strcat (buf, "sd\t{%2, %0|%0, %2}");
6979       return buf;
6980    }
6981   strcpy (buf, p);
6982
6983   switch (GET_CODE (operands[3]))
6984     {
6985     case MULT:
6986     case PLUS:
6987       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
6988         {
6989           rtx temp = operands[2];
6990           operands[2] = operands[1];
6991           operands[1] = temp;
6992         }
6993
6994       /* know operands[0] == operands[1].  */
6995
6996       if (GET_CODE (operands[2]) == MEM)
6997         {
6998           p = "%z2\t%2";
6999           break;
7000         }
7001
7002       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7003         {
7004           if (STACK_TOP_P (operands[0]))
7005             /* How is it that we are storing to a dead operand[2]?
7006                Well, presumably operands[1] is dead too.  We can't
7007                store the result to st(0) as st(0) gets popped on this
7008                instruction.  Instead store to operands[2] (which I
7009                think has to be st(1)).  st(1) will be popped later.
7010                gcc <= 2.8.1 didn't have this check and generated
7011                assembly code that the Unixware assembler rejected.  */
7012             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
7013           else
7014             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7015           break;
7016         }
7017
7018       if (STACK_TOP_P (operands[0]))
7019         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
7020       else
7021         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
7022       break;
7023
7024     case MINUS:
7025     case DIV:
7026       if (GET_CODE (operands[1]) == MEM)
7027         {
7028           p = "r%z1\t%1";
7029           break;
7030         }
7031
7032       if (GET_CODE (operands[2]) == MEM)
7033         {
7034           p = "%z2\t%2";
7035           break;
7036         }
7037
7038       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7039         {
7040 #if SYSV386_COMPAT
7041           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
7042              derived assemblers, confusingly reverse the direction of
7043              the operation for fsub{r} and fdiv{r} when the
7044              destination register is not st(0).  The Intel assembler
7045              doesn't have this brain damage.  Read !SYSV386_COMPAT to
7046              figure out what the hardware really does.  */
7047           if (STACK_TOP_P (operands[0]))
7048             p = "{p\t%0, %2|rp\t%2, %0}";
7049           else
7050             p = "{rp\t%2, %0|p\t%0, %2}";
7051 #else
7052           if (STACK_TOP_P (operands[0]))
7053             /* As above for fmul/fadd, we can't store to st(0).  */
7054             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
7055           else
7056             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7057 #endif
7058           break;
7059         }
7060
7061       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
7062         {
7063 #if SYSV386_COMPAT
7064           if (STACK_TOP_P (operands[0]))
7065             p = "{rp\t%0, %1|p\t%1, %0}";
7066           else
7067             p = "{p\t%1, %0|rp\t%0, %1}";
7068 #else
7069           if (STACK_TOP_P (operands[0]))
7070             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
7071           else
7072             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
7073 #endif
7074           break;
7075         }
7076
7077       if (STACK_TOP_P (operands[0]))
7078         {
7079           if (STACK_TOP_P (operands[1]))
7080             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
7081           else
7082             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
7083           break;
7084         }
7085       else if (STACK_TOP_P (operands[1]))
7086         {
7087 #if SYSV386_COMPAT
7088           p = "{\t%1, %0|r\t%0, %1}";
7089 #else
7090           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
7091 #endif
7092         }
7093       else
7094         {
7095 #if SYSV386_COMPAT
7096           p = "{r\t%2, %0|\t%0, %2}";
7097 #else
7098           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
7099 #endif
7100         }
7101       break;
7102
7103     default:
7104       abort ();
7105     }
7106
7107   strcat (buf, p);
7108   return buf;
7109 }
7110
7111 /* Output code to initialize control word copies used by trunc?f?i and
7112    rounding patterns.  CURRENT_MODE is set to current control word,
7113    while NEW_MODE is set to new control word.  */
7114
7115 void
7116 emit_i387_cw_initialization (rtx current_mode, rtx new_mode, int mode)
7117 {
7118   rtx reg = gen_reg_rtx (HImode);
7119
7120   emit_insn (gen_x86_fnstcw_1 (current_mode));
7121   emit_move_insn (reg, current_mode);
7122
7123   if (!TARGET_PARTIAL_REG_STALL && !optimize_size
7124       && !TARGET_64BIT)
7125     {
7126       switch (mode)
7127         {
7128         case I387_CW_FLOOR:
7129           /* round down toward -oo */
7130           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
7131           break;
7132
7133         case I387_CW_CEIL:
7134           /* round up toward +oo */
7135           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
7136           break;
7137
7138         case I387_CW_TRUNC:
7139           /* round toward zero (truncate) */
7140           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
7141           break;
7142  
7143         case I387_CW_MASK_PM:
7144           /* mask precision exception for nearbyint() */
7145           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
7146           break;
7147
7148         default:
7149           abort();
7150         }
7151     }
7152   else
7153     {
7154       switch (mode)
7155         {
7156         case I387_CW_FLOOR:
7157           /* round down toward -oo */
7158           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
7159           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
7160           break;
7161
7162         case I387_CW_CEIL:
7163           /* round up toward +oo */
7164           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
7165           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
7166           break;
7167
7168         case I387_CW_TRUNC:
7169           /* round toward zero (truncate) */
7170           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
7171           break;
7172
7173         case I387_CW_MASK_PM:
7174           /* mask precision exception for nearbyint() */
7175           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
7176           break;
7177
7178         default:
7179           abort();
7180         }
7181     }
7182
7183   emit_move_insn (new_mode, reg);
7184 }
7185
7186 /* Output code for INSN to convert a float to a signed int.  OPERANDS
7187    are the insn operands.  The output may be [HSD]Imode and the input
7188    operand may be [SDX]Fmode.  */
7189
7190 const char *
7191 output_fix_trunc (rtx insn, rtx *operands)
7192 {
7193   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
7194   int dimode_p = GET_MODE (operands[0]) == DImode;
7195
7196   /* Jump through a hoop or two for DImode, since the hardware has no
7197      non-popping instruction.  We used to do this a different way, but
7198      that was somewhat fragile and broke with post-reload splitters.  */
7199   if (dimode_p && !stack_top_dies)
7200     output_asm_insn ("fld\t%y1", operands);
7201
7202   if (!STACK_TOP_P (operands[1]))
7203     abort ();
7204
7205   if (GET_CODE (operands[0]) != MEM)
7206     abort ();
7207
7208   output_asm_insn ("fldcw\t%3", operands);
7209   if (stack_top_dies || dimode_p)
7210     output_asm_insn ("fistp%z0\t%0", operands);
7211   else
7212     output_asm_insn ("fist%z0\t%0", operands);
7213   output_asm_insn ("fldcw\t%2", operands);
7214
7215   return "";
7216 }
7217
7218 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
7219    should be used and 2 when fnstsw should be used.  UNORDERED_P is true
7220    when fucom should be used.  */
7221
7222 const char *
7223 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
7224 {
7225   int stack_top_dies;
7226   rtx cmp_op0 = operands[0];
7227   rtx cmp_op1 = operands[1];
7228   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]);
7229
7230   if (eflags_p == 2)
7231     {
7232       cmp_op0 = cmp_op1;
7233       cmp_op1 = operands[2];
7234     }
7235   if (is_sse)
7236     {
7237       if (GET_MODE (operands[0]) == SFmode)
7238         if (unordered_p)
7239           return "ucomiss\t{%1, %0|%0, %1}";
7240         else
7241           return "comiss\t{%1, %0|%0, %1}";
7242       else
7243         if (unordered_p)
7244           return "ucomisd\t{%1, %0|%0, %1}";
7245         else
7246           return "comisd\t{%1, %0|%0, %1}";
7247     }
7248
7249   if (! STACK_TOP_P (cmp_op0))
7250     abort ();
7251
7252   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
7253
7254   if (STACK_REG_P (cmp_op1)
7255       && stack_top_dies
7256       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
7257       && REGNO (cmp_op1) != FIRST_STACK_REG)
7258     {
7259       /* If both the top of the 387 stack dies, and the other operand
7260          is also a stack register that dies, then this must be a
7261          `fcompp' float compare */
7262
7263       if (eflags_p == 1)
7264         {
7265           /* There is no double popping fcomi variant.  Fortunately,
7266              eflags is immune from the fstp's cc clobbering.  */
7267           if (unordered_p)
7268             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
7269           else
7270             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
7271           return TARGET_USE_FFREEP ? "ffreep\t%y0" : "fstp\t%y0";
7272         }
7273       else
7274         {
7275           if (eflags_p == 2)
7276             {
7277               if (unordered_p)
7278                 return "fucompp\n\tfnstsw\t%0";
7279               else
7280                 return "fcompp\n\tfnstsw\t%0";
7281             }
7282           else
7283             {
7284               if (unordered_p)
7285                 return "fucompp";
7286               else
7287                 return "fcompp";
7288             }
7289         }
7290     }
7291   else
7292     {
7293       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
7294
7295       static const char * const alt[24] =
7296       {
7297         "fcom%z1\t%y1",
7298         "fcomp%z1\t%y1",
7299         "fucom%z1\t%y1",
7300         "fucomp%z1\t%y1",
7301
7302         "ficom%z1\t%y1",
7303         "ficomp%z1\t%y1",
7304         NULL,
7305         NULL,
7306
7307         "fcomi\t{%y1, %0|%0, %y1}",
7308         "fcomip\t{%y1, %0|%0, %y1}",
7309         "fucomi\t{%y1, %0|%0, %y1}",
7310         "fucomip\t{%y1, %0|%0, %y1}",
7311
7312         NULL,
7313         NULL,
7314         NULL,
7315         NULL,
7316
7317         "fcom%z2\t%y2\n\tfnstsw\t%0",
7318         "fcomp%z2\t%y2\n\tfnstsw\t%0",
7319         "fucom%z2\t%y2\n\tfnstsw\t%0",
7320         "fucomp%z2\t%y2\n\tfnstsw\t%0",
7321
7322         "ficom%z2\t%y2\n\tfnstsw\t%0",
7323         "ficomp%z2\t%y2\n\tfnstsw\t%0",
7324         NULL,
7325         NULL
7326       };
7327
7328       int mask;
7329       const char *ret;
7330
7331       mask  = eflags_p << 3;
7332       mask |= (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT) << 2;
7333       mask |= unordered_p << 1;
7334       mask |= stack_top_dies;
7335
7336       if (mask >= 24)
7337         abort ();
7338       ret = alt[mask];
7339       if (ret == NULL)
7340         abort ();
7341
7342       return ret;
7343     }
7344 }
7345
7346 void
7347 ix86_output_addr_vec_elt (FILE *file, int value)
7348 {
7349   const char *directive = ASM_LONG;
7350
7351   if (TARGET_64BIT)
7352     {
7353 #ifdef ASM_QUAD
7354       directive = ASM_QUAD;
7355 #else
7356       abort ();
7357 #endif
7358     }
7359
7360   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
7361 }
7362
7363 void
7364 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
7365 {
7366   if (TARGET_64BIT)
7367     fprintf (file, "%s%s%d-%s%d\n",
7368              ASM_LONG, LPREFIX, value, LPREFIX, rel);
7369   else if (HAVE_AS_GOTOFF_IN_DATA)
7370     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
7371 #if TARGET_MACHO
7372   else if (TARGET_MACHO)
7373     {
7374       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
7375       machopic_output_function_base_name (file);
7376       fprintf(file, "\n");
7377     }
7378 #endif
7379   else
7380     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
7381                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
7382 }
7383 \f
7384 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
7385    for the target.  */
7386
7387 void
7388 ix86_expand_clear (rtx dest)
7389 {
7390   rtx tmp;
7391
7392   /* We play register width games, which are only valid after reload.  */
7393   if (!reload_completed)
7394     abort ();
7395
7396   /* Avoid HImode and its attendant prefix byte.  */
7397   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
7398     dest = gen_rtx_REG (SImode, REGNO (dest));
7399
7400   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
7401
7402   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
7403   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
7404     {
7405       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
7406       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
7407     }
7408
7409   emit_insn (tmp);
7410 }
7411
7412 /* X is an unchanging MEM.  If it is a constant pool reference, return
7413    the constant pool rtx, else NULL.  */
7414
7415 rtx
7416 maybe_get_pool_constant (rtx x)
7417 {
7418   x = ix86_delegitimize_address (XEXP (x, 0));
7419
7420   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7421     return get_pool_constant (x);
7422
7423   return NULL_RTX;
7424 }
7425
7426 void
7427 ix86_expand_move (enum machine_mode mode, rtx operands[])
7428 {
7429   int strict = (reload_in_progress || reload_completed);
7430   rtx op0, op1;
7431   enum tls_model model;
7432
7433   op0 = operands[0];
7434   op1 = operands[1];
7435
7436   model = GET_CODE (op1) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (op1) : 0;
7437   if (model)
7438     {
7439       op1 = legitimize_tls_address (op1, model, true);
7440       op1 = force_operand (op1, op0);
7441       if (op1 == op0)
7442         return;
7443     }
7444
7445   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
7446     {
7447 #if TARGET_MACHO
7448       if (MACHOPIC_PURE)
7449         {
7450           rtx temp = ((reload_in_progress
7451                        || ((op0 && GET_CODE (op0) == REG)
7452                            && mode == Pmode))
7453                       ? op0 : gen_reg_rtx (Pmode));
7454           op1 = machopic_indirect_data_reference (op1, temp);
7455           op1 = machopic_legitimize_pic_address (op1, mode,
7456                                                  temp == op1 ? 0 : temp);
7457         }
7458       else if (MACHOPIC_INDIRECT)
7459         op1 = machopic_indirect_data_reference (op1, 0);
7460       if (op0 == op1)
7461         return;
7462 #else
7463       if (GET_CODE (op0) == MEM)
7464         op1 = force_reg (Pmode, op1);
7465       else
7466         op1 = legitimize_address (op1, op1, Pmode);
7467 #endif /* TARGET_MACHO */
7468     }
7469   else
7470     {
7471       if (GET_CODE (op0) == MEM
7472           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
7473               || !push_operand (op0, mode))
7474           && GET_CODE (op1) == MEM)
7475         op1 = force_reg (mode, op1);
7476
7477       if (push_operand (op0, mode)
7478           && ! general_no_elim_operand (op1, mode))
7479         op1 = copy_to_mode_reg (mode, op1);
7480
7481       /* Force large constants in 64bit compilation into register
7482          to get them CSEed.  */
7483       if (TARGET_64BIT && mode == DImode
7484           && immediate_operand (op1, mode)
7485           && !x86_64_zext_immediate_operand (op1, VOIDmode)
7486           && !register_operand (op0, mode)
7487           && optimize && !reload_completed && !reload_in_progress)
7488         op1 = copy_to_mode_reg (mode, op1);
7489
7490       if (FLOAT_MODE_P (mode))
7491         {
7492           /* If we are loading a floating point constant to a register,
7493              force the value to memory now, since we'll get better code
7494              out the back end.  */
7495
7496           if (strict)
7497             ;
7498           else if (GET_CODE (op1) == CONST_DOUBLE)
7499             {
7500               op1 = validize_mem (force_const_mem (mode, op1));
7501               if (!register_operand (op0, mode))
7502                 {
7503                   rtx temp = gen_reg_rtx (mode);
7504                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
7505                   emit_move_insn (op0, temp);
7506                   return;
7507                 }
7508             }
7509         }
7510     }
7511
7512   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
7513 }
7514
7515 void
7516 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
7517 {
7518   /* Force constants other than zero into memory.  We do not know how
7519      the instructions used to build constants modify the upper 64 bits
7520      of the register, once we have that information we may be able
7521      to handle some of them more efficiently.  */
7522   if ((reload_in_progress | reload_completed) == 0
7523       && register_operand (operands[0], mode)
7524       && CONSTANT_P (operands[1]) && operands[1] != CONST0_RTX (mode))
7525     operands[1] = validize_mem (force_const_mem (mode, operands[1]));
7526
7527   /* Make operand1 a register if it isn't already.  */
7528   if (!no_new_pseudos
7529       && !register_operand (operands[0], mode)
7530       && !register_operand (operands[1], mode))
7531     {
7532       rtx temp = force_reg (GET_MODE (operands[1]), operands[1]);
7533       emit_move_insn (operands[0], temp);
7534       return;
7535     }
7536
7537   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7538 }
7539
7540 /* Attempt to expand a binary operator.  Make the expansion closer to the
7541    actual machine, then just general_operand, which will allow 3 separate
7542    memory references (one output, two input) in a single insn.  */
7543
7544 void
7545 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
7546                              rtx operands[])
7547 {
7548   int matching_memory;
7549   rtx src1, src2, dst, op, clob;
7550
7551   dst = operands[0];
7552   src1 = operands[1];
7553   src2 = operands[2];
7554
7555   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
7556   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
7557       && (rtx_equal_p (dst, src2)
7558           || immediate_operand (src1, mode)))
7559     {
7560       rtx temp = src1;
7561       src1 = src2;
7562       src2 = temp;
7563     }
7564
7565   /* If the destination is memory, and we do not have matching source
7566      operands, do things in registers.  */
7567   matching_memory = 0;
7568   if (GET_CODE (dst) == MEM)
7569     {
7570       if (rtx_equal_p (dst, src1))
7571         matching_memory = 1;
7572       else if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
7573                && rtx_equal_p (dst, src2))
7574         matching_memory = 2;
7575       else
7576         dst = gen_reg_rtx (mode);
7577     }
7578
7579   /* Both source operands cannot be in memory.  */
7580   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
7581     {
7582       if (matching_memory != 2)
7583         src2 = force_reg (mode, src2);
7584       else
7585         src1 = force_reg (mode, src1);
7586     }
7587
7588   /* If the operation is not commutable, source 1 cannot be a constant
7589      or non-matching memory.  */
7590   if ((CONSTANT_P (src1)
7591        || (!matching_memory && GET_CODE (src1) == MEM))
7592       && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
7593     src1 = force_reg (mode, src1);
7594
7595   /* If optimizing, copy to regs to improve CSE */
7596   if (optimize && ! no_new_pseudos)
7597     {
7598       if (GET_CODE (dst) == MEM)
7599         dst = gen_reg_rtx (mode);
7600       if (GET_CODE (src1) == MEM)
7601         src1 = force_reg (mode, src1);
7602       if (GET_CODE (src2) == MEM)
7603         src2 = force_reg (mode, src2);
7604     }
7605
7606   /* Emit the instruction.  */
7607
7608   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
7609   if (reload_in_progress)
7610     {
7611       /* Reload doesn't know about the flags register, and doesn't know that
7612          it doesn't want to clobber it.  We can only do this with PLUS.  */
7613       if (code != PLUS)
7614         abort ();
7615       emit_insn (op);
7616     }
7617   else
7618     {
7619       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
7620       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
7621     }
7622
7623   /* Fix up the destination if needed.  */
7624   if (dst != operands[0])
7625     emit_move_insn (operands[0], dst);
7626 }
7627
7628 /* Return TRUE or FALSE depending on whether the binary operator meets the
7629    appropriate constraints.  */
7630
7631 int
7632 ix86_binary_operator_ok (enum rtx_code code,
7633                          enum machine_mode mode ATTRIBUTE_UNUSED,
7634                          rtx operands[3])
7635 {
7636   /* Both source operands cannot be in memory.  */
7637   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
7638     return 0;
7639   /* If the operation is not commutable, source 1 cannot be a constant.  */
7640   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
7641     return 0;
7642   /* If the destination is memory, we must have a matching source operand.  */
7643   if (GET_CODE (operands[0]) == MEM
7644       && ! (rtx_equal_p (operands[0], operands[1])
7645             || (GET_RTX_CLASS (code) == RTX_COMM_ARITH
7646                 && rtx_equal_p (operands[0], operands[2]))))
7647     return 0;
7648   /* If the operation is not commutable and the source 1 is memory, we must
7649      have a matching destination.  */
7650   if (GET_CODE (operands[1]) == MEM
7651       && GET_RTX_CLASS (code) != RTX_COMM_ARITH
7652       && ! rtx_equal_p (operands[0], operands[1]))
7653     return 0;
7654   return 1;
7655 }
7656
7657 /* Attempt to expand a unary operator.  Make the expansion closer to the
7658    actual machine, then just general_operand, which will allow 2 separate
7659    memory references (one output, one input) in a single insn.  */
7660
7661 void
7662 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
7663                             rtx operands[])
7664 {
7665   int matching_memory;
7666   rtx src, dst, op, clob;
7667
7668   dst = operands[0];
7669   src = operands[1];
7670
7671   /* If the destination is memory, and we do not have matching source
7672      operands, do things in registers.  */
7673   matching_memory = 0;
7674   if (GET_CODE (dst) == MEM)
7675     {
7676       if (rtx_equal_p (dst, src))
7677         matching_memory = 1;
7678       else
7679         dst = gen_reg_rtx (mode);
7680     }
7681
7682   /* When source operand is memory, destination must match.  */
7683   if (!matching_memory && GET_CODE (src) == MEM)
7684     src = force_reg (mode, src);
7685
7686   /* If optimizing, copy to regs to improve CSE */
7687   if (optimize && ! no_new_pseudos)
7688     {
7689       if (GET_CODE (dst) == MEM)
7690         dst = gen_reg_rtx (mode);
7691       if (GET_CODE (src) == MEM)
7692         src = force_reg (mode, src);
7693     }
7694
7695   /* Emit the instruction.  */
7696
7697   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
7698   if (reload_in_progress || code == NOT)
7699     {
7700       /* Reload doesn't know about the flags register, and doesn't know that
7701          it doesn't want to clobber it.  */
7702       if (code != NOT)
7703         abort ();
7704       emit_insn (op);
7705     }
7706   else
7707     {
7708       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
7709       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
7710     }
7711
7712   /* Fix up the destination if needed.  */
7713   if (dst != operands[0])
7714     emit_move_insn (operands[0], dst);
7715 }
7716
7717 /* Return TRUE or FALSE depending on whether the unary operator meets the
7718    appropriate constraints.  */
7719
7720 int
7721 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
7722                         enum machine_mode mode ATTRIBUTE_UNUSED,
7723                         rtx operands[2] ATTRIBUTE_UNUSED)
7724 {
7725   /* If one of operands is memory, source and destination must match.  */
7726   if ((GET_CODE (operands[0]) == MEM
7727        || GET_CODE (operands[1]) == MEM)
7728       && ! rtx_equal_p (operands[0], operands[1]))
7729     return FALSE;
7730   return TRUE;
7731 }
7732
7733 /* Return TRUE or FALSE depending on whether the first SET in INSN
7734    has source and destination with matching CC modes, and that the
7735    CC mode is at least as constrained as REQ_MODE.  */
7736
7737 int
7738 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
7739 {
7740   rtx set;
7741   enum machine_mode set_mode;
7742
7743   set = PATTERN (insn);
7744   if (GET_CODE (set) == PARALLEL)
7745     set = XVECEXP (set, 0, 0);
7746   if (GET_CODE (set) != SET)
7747     abort ();
7748   if (GET_CODE (SET_SRC (set)) != COMPARE)
7749     abort ();
7750
7751   set_mode = GET_MODE (SET_DEST (set));
7752   switch (set_mode)
7753     {
7754     case CCNOmode:
7755       if (req_mode != CCNOmode
7756           && (req_mode != CCmode
7757               || XEXP (SET_SRC (set), 1) != const0_rtx))
7758         return 0;
7759       break;
7760     case CCmode:
7761       if (req_mode == CCGCmode)
7762         return 0;
7763       /* FALLTHRU */
7764     case CCGCmode:
7765       if (req_mode == CCGOCmode || req_mode == CCNOmode)
7766         return 0;
7767       /* FALLTHRU */
7768     case CCGOCmode:
7769       if (req_mode == CCZmode)
7770         return 0;
7771       /* FALLTHRU */
7772     case CCZmode:
7773       break;
7774
7775     default:
7776       abort ();
7777     }
7778
7779   return (GET_MODE (SET_SRC (set)) == set_mode);
7780 }
7781
7782 /* Generate insn patterns to do an integer compare of OPERANDS.  */
7783
7784 static rtx
7785 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
7786 {
7787   enum machine_mode cmpmode;
7788   rtx tmp, flags;
7789
7790   cmpmode = SELECT_CC_MODE (code, op0, op1);
7791   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
7792
7793   /* This is very simple, but making the interface the same as in the
7794      FP case makes the rest of the code easier.  */
7795   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
7796   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
7797
7798   /* Return the test that should be put into the flags user, i.e.
7799      the bcc, scc, or cmov instruction.  */
7800   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
7801 }
7802
7803 /* Figure out whether to use ordered or unordered fp comparisons.
7804    Return the appropriate mode to use.  */
7805
7806 enum machine_mode
7807 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
7808 {
7809   /* ??? In order to make all comparisons reversible, we do all comparisons
7810      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
7811      all forms trapping and nontrapping comparisons, we can make inequality
7812      comparisons trapping again, since it results in better code when using
7813      FCOM based compares.  */
7814   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
7815 }
7816
7817 enum machine_mode
7818 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
7819 {
7820   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7821     return ix86_fp_compare_mode (code);
7822   switch (code)
7823     {
7824       /* Only zero flag is needed.  */
7825     case EQ:                    /* ZF=0 */
7826     case NE:                    /* ZF!=0 */
7827       return CCZmode;
7828       /* Codes needing carry flag.  */
7829     case GEU:                   /* CF=0 */
7830     case GTU:                   /* CF=0 & ZF=0 */
7831     case LTU:                   /* CF=1 */
7832     case LEU:                   /* CF=1 | ZF=1 */
7833       return CCmode;
7834       /* Codes possibly doable only with sign flag when
7835          comparing against zero.  */
7836     case GE:                    /* SF=OF   or   SF=0 */
7837     case LT:                    /* SF<>OF  or   SF=1 */
7838       if (op1 == const0_rtx)
7839         return CCGOCmode;
7840       else
7841         /* For other cases Carry flag is not required.  */
7842         return CCGCmode;
7843       /* Codes doable only with sign flag when comparing
7844          against zero, but we miss jump instruction for it
7845          so we need to use relational tests against overflow
7846          that thus needs to be zero.  */
7847     case GT:                    /* ZF=0 & SF=OF */
7848     case LE:                    /* ZF=1 | SF<>OF */
7849       if (op1 == const0_rtx)
7850         return CCNOmode;
7851       else
7852         return CCGCmode;
7853       /* strcmp pattern do (use flags) and combine may ask us for proper
7854          mode.  */
7855     case USE:
7856       return CCmode;
7857     default:
7858       abort ();
7859     }
7860 }
7861
7862 /* Return the fixed registers used for condition codes.  */
7863
7864 static bool
7865 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
7866 {
7867   *p1 = FLAGS_REG;
7868   *p2 = FPSR_REG;
7869   return true;
7870 }
7871
7872 /* If two condition code modes are compatible, return a condition code
7873    mode which is compatible with both.  Otherwise, return
7874    VOIDmode.  */
7875
7876 static enum machine_mode
7877 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
7878 {
7879   if (m1 == m2)
7880     return m1;
7881
7882   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
7883     return VOIDmode;
7884
7885   if ((m1 == CCGCmode && m2 == CCGOCmode)
7886       || (m1 == CCGOCmode && m2 == CCGCmode))
7887     return CCGCmode;
7888
7889   switch (m1)
7890     {
7891     default:
7892       abort ();
7893
7894     case CCmode:
7895     case CCGCmode:
7896     case CCGOCmode:
7897     case CCNOmode:
7898     case CCZmode:
7899       switch (m2)
7900         {
7901         default:
7902           return VOIDmode;
7903
7904         case CCmode:
7905         case CCGCmode:
7906         case CCGOCmode:
7907         case CCNOmode:
7908         case CCZmode:
7909           return CCmode;
7910         }
7911
7912     case CCFPmode:
7913     case CCFPUmode:
7914       /* These are only compatible with themselves, which we already
7915          checked above.  */
7916       return VOIDmode;
7917     }
7918 }
7919
7920 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
7921
7922 int
7923 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
7924 {
7925   enum rtx_code swapped_code = swap_condition (code);
7926   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
7927           || (ix86_fp_comparison_cost (swapped_code)
7928               == ix86_fp_comparison_fcomi_cost (swapped_code)));
7929 }
7930
7931 /* Swap, force into registers, or otherwise massage the two operands
7932    to a fp comparison.  The operands are updated in place; the new
7933    comparison code is returned.  */
7934
7935 static enum rtx_code
7936 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
7937 {
7938   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
7939   rtx op0 = *pop0, op1 = *pop1;
7940   enum machine_mode op_mode = GET_MODE (op0);
7941   int is_sse = SSE_REG_P (op0) | SSE_REG_P (op1);
7942
7943   /* All of the unordered compare instructions only work on registers.
7944      The same is true of the XFmode compare instructions.  The same is
7945      true of the fcomi compare instructions.  */
7946
7947   if (!is_sse
7948       && (fpcmp_mode == CCFPUmode
7949           || op_mode == XFmode
7950           || ix86_use_fcomi_compare (code)))
7951     {
7952       op0 = force_reg (op_mode, op0);
7953       op1 = force_reg (op_mode, op1);
7954     }
7955   else
7956     {
7957       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
7958          things around if they appear profitable, otherwise force op0
7959          into a register.  */
7960
7961       if (standard_80387_constant_p (op0) == 0
7962           || (GET_CODE (op0) == MEM
7963               && ! (standard_80387_constant_p (op1) == 0
7964                     || GET_CODE (op1) == MEM)))
7965         {
7966           rtx tmp;
7967           tmp = op0, op0 = op1, op1 = tmp;
7968           code = swap_condition (code);
7969         }
7970
7971       if (GET_CODE (op0) != REG)
7972         op0 = force_reg (op_mode, op0);
7973
7974       if (CONSTANT_P (op1))
7975         {
7976           if (standard_80387_constant_p (op1))
7977             op1 = force_reg (op_mode, op1);
7978           else
7979             op1 = validize_mem (force_const_mem (op_mode, op1));
7980         }
7981     }
7982
7983   /* Try to rearrange the comparison to make it cheaper.  */
7984   if (ix86_fp_comparison_cost (code)
7985       > ix86_fp_comparison_cost (swap_condition (code))
7986       && (GET_CODE (op1) == REG || !no_new_pseudos))
7987     {
7988       rtx tmp;
7989       tmp = op0, op0 = op1, op1 = tmp;
7990       code = swap_condition (code);
7991       if (GET_CODE (op0) != REG)
7992         op0 = force_reg (op_mode, op0);
7993     }
7994
7995   *pop0 = op0;
7996   *pop1 = op1;
7997   return code;
7998 }
7999
8000 /* Convert comparison codes we use to represent FP comparison to integer
8001    code that will result in proper branch.  Return UNKNOWN if no such code
8002    is available.  */
8003
8004 enum rtx_code
8005 ix86_fp_compare_code_to_integer (enum rtx_code code)
8006 {
8007   switch (code)
8008     {
8009     case GT:
8010       return GTU;
8011     case GE:
8012       return GEU;
8013     case ORDERED:
8014     case UNORDERED:
8015       return code;
8016       break;
8017     case UNEQ:
8018       return EQ;
8019       break;
8020     case UNLT:
8021       return LTU;
8022       break;
8023     case UNLE:
8024       return LEU;
8025       break;
8026     case LTGT:
8027       return NE;
8028       break;
8029     default:
8030       return UNKNOWN;
8031     }
8032 }
8033
8034 /* Split comparison code CODE into comparisons we can do using branch
8035    instructions.  BYPASS_CODE is comparison code for branch that will
8036    branch around FIRST_CODE and SECOND_CODE.  If some of branches
8037    is not required, set value to UNKNOWN.
8038    We never require more than two branches.  */
8039
8040 void
8041 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
8042                           enum rtx_code *first_code,
8043                           enum rtx_code *second_code)
8044 {
8045   *first_code = code;
8046   *bypass_code = UNKNOWN;
8047   *second_code = UNKNOWN;
8048
8049   /* The fcomi comparison sets flags as follows:
8050
8051      cmp    ZF PF CF
8052      >      0  0  0
8053      <      0  0  1
8054      =      1  0  0
8055      un     1  1  1 */
8056
8057   switch (code)
8058     {
8059     case GT:                    /* GTU - CF=0 & ZF=0 */
8060     case GE:                    /* GEU - CF=0 */
8061     case ORDERED:               /* PF=0 */
8062     case UNORDERED:             /* PF=1 */
8063     case UNEQ:                  /* EQ - ZF=1 */
8064     case UNLT:                  /* LTU - CF=1 */
8065     case UNLE:                  /* LEU - CF=1 | ZF=1 */
8066     case LTGT:                  /* EQ - ZF=0 */
8067       break;
8068     case LT:                    /* LTU - CF=1 - fails on unordered */
8069       *first_code = UNLT;
8070       *bypass_code = UNORDERED;
8071       break;
8072     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
8073       *first_code = UNLE;
8074       *bypass_code = UNORDERED;
8075       break;
8076     case EQ:                    /* EQ - ZF=1 - fails on unordered */
8077       *first_code = UNEQ;
8078       *bypass_code = UNORDERED;
8079       break;
8080     case NE:                    /* NE - ZF=0 - fails on unordered */
8081       *first_code = LTGT;
8082       *second_code = UNORDERED;
8083       break;
8084     case UNGE:                  /* GEU - CF=0 - fails on unordered */
8085       *first_code = GE;
8086       *second_code = UNORDERED;
8087       break;
8088     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
8089       *first_code = GT;
8090       *second_code = UNORDERED;
8091       break;
8092     default:
8093       abort ();
8094     }
8095   if (!TARGET_IEEE_FP)
8096     {
8097       *second_code = UNKNOWN;
8098       *bypass_code = UNKNOWN;
8099     }
8100 }
8101
8102 /* Return cost of comparison done fcom + arithmetics operations on AX.
8103    All following functions do use number of instructions as a cost metrics.
8104    In future this should be tweaked to compute bytes for optimize_size and
8105    take into account performance of various instructions on various CPUs.  */
8106 static int
8107 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
8108 {
8109   if (!TARGET_IEEE_FP)
8110     return 4;
8111   /* The cost of code output by ix86_expand_fp_compare.  */
8112   switch (code)
8113     {
8114     case UNLE:
8115     case UNLT:
8116     case LTGT:
8117     case GT:
8118     case GE:
8119     case UNORDERED:
8120     case ORDERED:
8121     case UNEQ:
8122       return 4;
8123       break;
8124     case LT:
8125     case NE:
8126     case EQ:
8127     case UNGE:
8128       return 5;
8129       break;
8130     case LE:
8131     case UNGT:
8132       return 6;
8133       break;
8134     default:
8135       abort ();
8136     }
8137 }
8138
8139 /* Return cost of comparison done using fcomi operation.
8140    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8141 static int
8142 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
8143 {
8144   enum rtx_code bypass_code, first_code, second_code;
8145   /* Return arbitrarily high cost when instruction is not supported - this
8146      prevents gcc from using it.  */
8147   if (!TARGET_CMOVE)
8148     return 1024;
8149   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8150   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
8151 }
8152
8153 /* Return cost of comparison done using sahf operation.
8154    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8155 static int
8156 ix86_fp_comparison_sahf_cost (enum rtx_code code)
8157 {
8158   enum rtx_code bypass_code, first_code, second_code;
8159   /* Return arbitrarily high cost when instruction is not preferred - this
8160      avoids gcc from using it.  */
8161   if (!TARGET_USE_SAHF && !optimize_size)
8162     return 1024;
8163   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8164   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
8165 }
8166
8167 /* Compute cost of the comparison done using any method.
8168    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8169 static int
8170 ix86_fp_comparison_cost (enum rtx_code code)
8171 {
8172   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
8173   int min;
8174
8175   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
8176   sahf_cost = ix86_fp_comparison_sahf_cost (code);
8177
8178   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
8179   if (min > sahf_cost)
8180     min = sahf_cost;
8181   if (min > fcomi_cost)
8182     min = fcomi_cost;
8183   return min;
8184 }
8185
8186 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
8187
8188 static rtx
8189 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
8190                         rtx *second_test, rtx *bypass_test)
8191 {
8192   enum machine_mode fpcmp_mode, intcmp_mode;
8193   rtx tmp, tmp2;
8194   int cost = ix86_fp_comparison_cost (code);
8195   enum rtx_code bypass_code, first_code, second_code;
8196
8197   fpcmp_mode = ix86_fp_compare_mode (code);
8198   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
8199
8200   if (second_test)
8201     *second_test = NULL_RTX;
8202   if (bypass_test)
8203     *bypass_test = NULL_RTX;
8204
8205   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8206
8207   /* Do fcomi/sahf based test when profitable.  */
8208   if ((bypass_code == UNKNOWN || bypass_test)
8209       && (second_code == UNKNOWN || second_test)
8210       && ix86_fp_comparison_arithmetics_cost (code) > cost)
8211     {
8212       if (TARGET_CMOVE)
8213         {
8214           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8215           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
8216                              tmp);
8217           emit_insn (tmp);
8218         }
8219       else
8220         {
8221           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8222           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
8223           if (!scratch)
8224             scratch = gen_reg_rtx (HImode);
8225           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
8226           emit_insn (gen_x86_sahf_1 (scratch));
8227         }
8228
8229       /* The FP codes work out to act like unsigned.  */
8230       intcmp_mode = fpcmp_mode;
8231       code = first_code;
8232       if (bypass_code != UNKNOWN)
8233         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
8234                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
8235                                        const0_rtx);
8236       if (second_code != UNKNOWN)
8237         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
8238                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
8239                                        const0_rtx);
8240     }
8241   else
8242     {
8243       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
8244       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8245       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
8246       if (!scratch)
8247         scratch = gen_reg_rtx (HImode);
8248       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
8249
8250       /* In the unordered case, we have to check C2 for NaN's, which
8251          doesn't happen to work out to anything nice combination-wise.
8252          So do some bit twiddling on the value we've got in AH to come
8253          up with an appropriate set of condition codes.  */
8254
8255       intcmp_mode = CCNOmode;
8256       switch (code)
8257         {
8258         case GT:
8259         case UNGT:
8260           if (code == GT || !TARGET_IEEE_FP)
8261             {
8262               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
8263               code = EQ;
8264             }
8265           else
8266             {
8267               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8268               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
8269               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
8270               intcmp_mode = CCmode;
8271               code = GEU;
8272             }
8273           break;
8274         case LT:
8275         case UNLT:
8276           if (code == LT && TARGET_IEEE_FP)
8277             {
8278               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8279               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
8280               intcmp_mode = CCmode;
8281               code = EQ;
8282             }
8283           else
8284             {
8285               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
8286               code = NE;
8287             }
8288           break;
8289         case GE:
8290         case UNGE:
8291           if (code == GE || !TARGET_IEEE_FP)
8292             {
8293               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
8294               code = EQ;
8295             }
8296           else
8297             {
8298               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8299               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
8300                                              GEN_INT (0x01)));
8301               code = NE;
8302             }
8303           break;
8304         case LE:
8305         case UNLE:
8306           if (code == LE && TARGET_IEEE_FP)
8307             {
8308               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8309               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
8310               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
8311               intcmp_mode = CCmode;
8312               code = LTU;
8313             }
8314           else
8315             {
8316               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
8317               code = NE;
8318             }
8319           break;
8320         case EQ:
8321         case UNEQ:
8322           if (code == EQ && TARGET_IEEE_FP)
8323             {
8324               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8325               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
8326               intcmp_mode = CCmode;
8327               code = EQ;
8328             }
8329           else
8330             {
8331               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
8332               code = NE;
8333               break;
8334             }
8335           break;
8336         case NE:
8337         case LTGT:
8338           if (code == NE && TARGET_IEEE_FP)
8339             {
8340               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8341               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
8342                                              GEN_INT (0x40)));
8343               code = NE;
8344             }
8345           else
8346             {
8347               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
8348               code = EQ;
8349             }
8350           break;
8351
8352         case UNORDERED:
8353           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
8354           code = NE;
8355           break;
8356         case ORDERED:
8357           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
8358           code = EQ;
8359           break;
8360
8361         default:
8362           abort ();
8363         }
8364     }
8365
8366   /* Return the test that should be put into the flags user, i.e.
8367      the bcc, scc, or cmov instruction.  */
8368   return gen_rtx_fmt_ee (code, VOIDmode,
8369                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
8370                          const0_rtx);
8371 }
8372
8373 rtx
8374 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
8375 {
8376   rtx op0, op1, ret;
8377   op0 = ix86_compare_op0;
8378   op1 = ix86_compare_op1;
8379
8380   if (second_test)
8381     *second_test = NULL_RTX;
8382   if (bypass_test)
8383     *bypass_test = NULL_RTX;
8384
8385   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8386     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
8387                                   second_test, bypass_test);
8388   else
8389     ret = ix86_expand_int_compare (code, op0, op1);
8390
8391   return ret;
8392 }
8393
8394 /* Return true if the CODE will result in nontrivial jump sequence.  */
8395 bool
8396 ix86_fp_jump_nontrivial_p (enum rtx_code code)
8397 {
8398   enum rtx_code bypass_code, first_code, second_code;
8399   if (!TARGET_CMOVE)
8400     return true;
8401   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8402   return bypass_code != UNKNOWN || second_code != UNKNOWN;
8403 }
8404
8405 void
8406 ix86_expand_branch (enum rtx_code code, rtx label)
8407 {
8408   rtx tmp;
8409
8410   switch (GET_MODE (ix86_compare_op0))
8411     {
8412     case QImode:
8413     case HImode:
8414     case SImode:
8415       simple:
8416       tmp = ix86_expand_compare (code, NULL, NULL);
8417       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
8418                                   gen_rtx_LABEL_REF (VOIDmode, label),
8419                                   pc_rtx);
8420       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
8421       return;
8422
8423     case SFmode:
8424     case DFmode:
8425     case XFmode:
8426       {
8427         rtvec vec;
8428         int use_fcomi;
8429         enum rtx_code bypass_code, first_code, second_code;
8430
8431         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
8432                                              &ix86_compare_op1);
8433
8434         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8435
8436         /* Check whether we will use the natural sequence with one jump.  If
8437            so, we can expand jump early.  Otherwise delay expansion by
8438            creating compound insn to not confuse optimizers.  */
8439         if (bypass_code == UNKNOWN && second_code == UNKNOWN
8440             && TARGET_CMOVE)
8441           {
8442             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
8443                                   gen_rtx_LABEL_REF (VOIDmode, label),
8444                                   pc_rtx, NULL_RTX);
8445           }
8446         else
8447           {
8448             tmp = gen_rtx_fmt_ee (code, VOIDmode,
8449                                   ix86_compare_op0, ix86_compare_op1);
8450             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
8451                                         gen_rtx_LABEL_REF (VOIDmode, label),
8452                                         pc_rtx);
8453             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
8454
8455             use_fcomi = ix86_use_fcomi_compare (code);
8456             vec = rtvec_alloc (3 + !use_fcomi);
8457             RTVEC_ELT (vec, 0) = tmp;
8458             RTVEC_ELT (vec, 1)
8459               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
8460             RTVEC_ELT (vec, 2)
8461               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
8462             if (! use_fcomi)
8463               RTVEC_ELT (vec, 3)
8464                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
8465
8466             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
8467           }
8468         return;
8469       }
8470
8471     case DImode:
8472       if (TARGET_64BIT)
8473         goto simple;
8474       /* Expand DImode branch into multiple compare+branch.  */
8475       {
8476         rtx lo[2], hi[2], label2;
8477         enum rtx_code code1, code2, code3;
8478
8479         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
8480           {
8481             tmp = ix86_compare_op0;
8482             ix86_compare_op0 = ix86_compare_op1;
8483             ix86_compare_op1 = tmp;
8484             code = swap_condition (code);
8485           }
8486         split_di (&ix86_compare_op0, 1, lo+0, hi+0);
8487         split_di (&ix86_compare_op1, 1, lo+1, hi+1);
8488
8489         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
8490            avoid two branches.  This costs one extra insn, so disable when
8491            optimizing for size.  */
8492
8493         if ((code == EQ || code == NE)
8494             && (!optimize_size
8495                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
8496           {
8497             rtx xor0, xor1;
8498
8499             xor1 = hi[0];
8500             if (hi[1] != const0_rtx)
8501               xor1 = expand_binop (SImode, xor_optab, xor1, hi[1],
8502                                    NULL_RTX, 0, OPTAB_WIDEN);
8503
8504             xor0 = lo[0];
8505             if (lo[1] != const0_rtx)
8506               xor0 = expand_binop (SImode, xor_optab, xor0, lo[1],
8507                                    NULL_RTX, 0, OPTAB_WIDEN);
8508
8509             tmp = expand_binop (SImode, ior_optab, xor1, xor0,
8510                                 NULL_RTX, 0, OPTAB_WIDEN);
8511
8512             ix86_compare_op0 = tmp;
8513             ix86_compare_op1 = const0_rtx;
8514             ix86_expand_branch (code, label);
8515             return;
8516           }
8517
8518         /* Otherwise, if we are doing less-than or greater-or-equal-than,
8519            op1 is a constant and the low word is zero, then we can just
8520            examine the high word.  */
8521
8522         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
8523           switch (code)
8524             {
8525             case LT: case LTU: case GE: case GEU:
8526               ix86_compare_op0 = hi[0];
8527               ix86_compare_op1 = hi[1];
8528               ix86_expand_branch (code, label);
8529               return;
8530             default:
8531               break;
8532             }
8533
8534         /* Otherwise, we need two or three jumps.  */
8535
8536         label2 = gen_label_rtx ();
8537
8538         code1 = code;
8539         code2 = swap_condition (code);
8540         code3 = unsigned_condition (code);
8541
8542         switch (code)
8543           {
8544           case LT: case GT: case LTU: case GTU:
8545             break;
8546
8547           case LE:   code1 = LT;  code2 = GT;  break;
8548           case GE:   code1 = GT;  code2 = LT;  break;
8549           case LEU:  code1 = LTU; code2 = GTU; break;
8550           case GEU:  code1 = GTU; code2 = LTU; break;
8551
8552           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
8553           case NE:   code2 = UNKNOWN; break;
8554
8555           default:
8556             abort ();
8557           }
8558
8559         /*
8560          * a < b =>
8561          *    if (hi(a) < hi(b)) goto true;
8562          *    if (hi(a) > hi(b)) goto false;
8563          *    if (lo(a) < lo(b)) goto true;
8564          *  false:
8565          */
8566
8567         ix86_compare_op0 = hi[0];
8568         ix86_compare_op1 = hi[1];
8569
8570         if (code1 != UNKNOWN)
8571           ix86_expand_branch (code1, label);
8572         if (code2 != UNKNOWN)
8573           ix86_expand_branch (code2, label2);
8574
8575         ix86_compare_op0 = lo[0];
8576         ix86_compare_op1 = lo[1];
8577         ix86_expand_branch (code3, label);
8578
8579         if (code2 != UNKNOWN)
8580           emit_label (label2);
8581         return;
8582       }
8583
8584     default:
8585       abort ();
8586     }
8587 }
8588
8589 /* Split branch based on floating point condition.  */
8590 void
8591 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
8592                       rtx target1, rtx target2, rtx tmp)
8593 {
8594   rtx second, bypass;
8595   rtx label = NULL_RTX;
8596   rtx condition;
8597   int bypass_probability = -1, second_probability = -1, probability = -1;
8598   rtx i;
8599
8600   if (target2 != pc_rtx)
8601     {
8602       rtx tmp = target2;
8603       code = reverse_condition_maybe_unordered (code);
8604       target2 = target1;
8605       target1 = tmp;
8606     }
8607
8608   condition = ix86_expand_fp_compare (code, op1, op2,
8609                                       tmp, &second, &bypass);
8610
8611   if (split_branch_probability >= 0)
8612     {
8613       /* Distribute the probabilities across the jumps.
8614          Assume the BYPASS and SECOND to be always test
8615          for UNORDERED.  */
8616       probability = split_branch_probability;
8617
8618       /* Value of 1 is low enough to make no need for probability
8619          to be updated.  Later we may run some experiments and see
8620          if unordered values are more frequent in practice.  */
8621       if (bypass)
8622         bypass_probability = 1;
8623       if (second)
8624         second_probability = 1;
8625     }
8626   if (bypass != NULL_RTX)
8627     {
8628       label = gen_label_rtx ();
8629       i = emit_jump_insn (gen_rtx_SET
8630                           (VOIDmode, pc_rtx,
8631                            gen_rtx_IF_THEN_ELSE (VOIDmode,
8632                                                  bypass,
8633                                                  gen_rtx_LABEL_REF (VOIDmode,
8634                                                                     label),
8635                                                  pc_rtx)));
8636       if (bypass_probability >= 0)
8637         REG_NOTES (i)
8638           = gen_rtx_EXPR_LIST (REG_BR_PROB,
8639                                GEN_INT (bypass_probability),
8640                                REG_NOTES (i));
8641     }
8642   i = emit_jump_insn (gen_rtx_SET
8643                       (VOIDmode, pc_rtx,
8644                        gen_rtx_IF_THEN_ELSE (VOIDmode,
8645                                              condition, target1, target2)));
8646   if (probability >= 0)
8647     REG_NOTES (i)
8648       = gen_rtx_EXPR_LIST (REG_BR_PROB,
8649                            GEN_INT (probability),
8650                            REG_NOTES (i));
8651   if (second != NULL_RTX)
8652     {
8653       i = emit_jump_insn (gen_rtx_SET
8654                           (VOIDmode, pc_rtx,
8655                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
8656                                                  target2)));
8657       if (second_probability >= 0)
8658         REG_NOTES (i)
8659           = gen_rtx_EXPR_LIST (REG_BR_PROB,
8660                                GEN_INT (second_probability),
8661                                REG_NOTES (i));
8662     }
8663   if (label != NULL_RTX)
8664     emit_label (label);
8665 }
8666
8667 int
8668 ix86_expand_setcc (enum rtx_code code, rtx dest)
8669 {
8670   rtx ret, tmp, tmpreg, equiv;
8671   rtx second_test, bypass_test;
8672
8673   if (GET_MODE (ix86_compare_op0) == DImode
8674       && !TARGET_64BIT)
8675     return 0; /* FAIL */
8676
8677   if (GET_MODE (dest) != QImode)
8678     abort ();
8679
8680   ret = ix86_expand_compare (code, &second_test, &bypass_test);
8681   PUT_MODE (ret, QImode);
8682
8683   tmp = dest;
8684   tmpreg = dest;
8685
8686   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
8687   if (bypass_test || second_test)
8688     {
8689       rtx test = second_test;
8690       int bypass = 0;
8691       rtx tmp2 = gen_reg_rtx (QImode);
8692       if (bypass_test)
8693         {
8694           if (second_test)
8695             abort ();
8696           test = bypass_test;
8697           bypass = 1;
8698           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
8699         }
8700       PUT_MODE (test, QImode);
8701       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
8702
8703       if (bypass)
8704         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
8705       else
8706         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
8707     }
8708
8709   /* Attach a REG_EQUAL note describing the comparison result.  */
8710   equiv = simplify_gen_relational (code, QImode,
8711                                    GET_MODE (ix86_compare_op0),
8712                                    ix86_compare_op0, ix86_compare_op1);
8713   set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
8714
8715   return 1; /* DONE */
8716 }
8717
8718 /* Expand comparison setting or clearing carry flag.  Return true when
8719    successful and set pop for the operation.  */
8720 static bool
8721 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
8722 {
8723   enum machine_mode mode =
8724     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
8725
8726   /* Do not handle DImode compares that go trought special path.  Also we can't
8727      deal with FP compares yet.  This is possible to add.  */
8728   if ((mode == DImode && !TARGET_64BIT))
8729     return false;
8730   if (FLOAT_MODE_P (mode))
8731     {
8732       rtx second_test = NULL, bypass_test = NULL;
8733       rtx compare_op, compare_seq;
8734
8735       /* Shortcut:  following common codes never translate into carry flag compares.  */
8736       if (code == EQ || code == NE || code == UNEQ || code == LTGT
8737           || code == ORDERED || code == UNORDERED)
8738         return false;
8739
8740       /* These comparisons require zero flag; swap operands so they won't.  */
8741       if ((code == GT || code == UNLE || code == LE || code == UNGT)
8742           && !TARGET_IEEE_FP)
8743         {
8744           rtx tmp = op0;
8745           op0 = op1;
8746           op1 = tmp;
8747           code = swap_condition (code);
8748         }
8749
8750       /* Try to expand the comparison and verify that we end up with carry flag
8751          based comparison.  This is fails to be true only when we decide to expand
8752          comparison using arithmetic that is not too common scenario.  */
8753       start_sequence ();
8754       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
8755                                            &second_test, &bypass_test);
8756       compare_seq = get_insns ();
8757       end_sequence ();
8758
8759       if (second_test || bypass_test)
8760         return false;
8761       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
8762           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
8763         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
8764       else
8765         code = GET_CODE (compare_op);
8766       if (code != LTU && code != GEU)
8767         return false;
8768       emit_insn (compare_seq);
8769       *pop = compare_op;
8770       return true;
8771     }
8772   if (!INTEGRAL_MODE_P (mode))
8773     return false;
8774   switch (code)
8775     {
8776     case LTU:
8777     case GEU:
8778       break;
8779
8780     /* Convert a==0 into (unsigned)a<1.  */
8781     case EQ:
8782     case NE:
8783       if (op1 != const0_rtx)
8784         return false;
8785       op1 = const1_rtx;
8786       code = (code == EQ ? LTU : GEU);
8787       break;
8788
8789     /* Convert a>b into b<a or a>=b-1.  */
8790     case GTU:
8791     case LEU:
8792       if (GET_CODE (op1) == CONST_INT)
8793         {
8794           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
8795           /* Bail out on overflow.  We still can swap operands but that
8796              would force loading of the constant into register.  */
8797           if (op1 == const0_rtx
8798               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
8799             return false;
8800           code = (code == GTU ? GEU : LTU);
8801         }
8802       else
8803         {
8804           rtx tmp = op1;
8805           op1 = op0;
8806           op0 = tmp;
8807           code = (code == GTU ? LTU : GEU);
8808         }
8809       break;
8810
8811     /* Convert a>=0 into (unsigned)a<0x80000000.  */
8812     case LT:
8813     case GE:
8814       if (mode == DImode || op1 != const0_rtx)
8815         return false;
8816       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
8817       code = (code == LT ? GEU : LTU);
8818       break;
8819     case LE:
8820     case GT:
8821       if (mode == DImode || op1 != constm1_rtx)
8822         return false;
8823       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
8824       code = (code == LE ? GEU : LTU);
8825       break;
8826
8827     default:
8828       return false;
8829     }
8830   /* Swapping operands may cause constant to appear as first operand.  */
8831   if (!nonimmediate_operand (op0, VOIDmode))
8832     {
8833       if (no_new_pseudos)
8834         return false;
8835       op0 = force_reg (mode, op0);
8836     }
8837   ix86_compare_op0 = op0;
8838   ix86_compare_op1 = op1;
8839   *pop = ix86_expand_compare (code, NULL, NULL);
8840   if (GET_CODE (*pop) != LTU && GET_CODE (*pop) != GEU)
8841     abort ();
8842   return true;
8843 }
8844
8845 int
8846 ix86_expand_int_movcc (rtx operands[])
8847 {
8848   enum rtx_code code = GET_CODE (operands[1]), compare_code;
8849   rtx compare_seq, compare_op;
8850   rtx second_test, bypass_test;
8851   enum machine_mode mode = GET_MODE (operands[0]);
8852   bool sign_bit_compare_p = false;;
8853
8854   start_sequence ();
8855   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
8856   compare_seq = get_insns ();
8857   end_sequence ();
8858
8859   compare_code = GET_CODE (compare_op);
8860
8861   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
8862       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
8863     sign_bit_compare_p = true;
8864
8865   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
8866      HImode insns, we'd be swallowed in word prefix ops.  */
8867
8868   if ((mode != HImode || TARGET_FAST_PREFIX)
8869       && (mode != DImode || TARGET_64BIT)
8870       && GET_CODE (operands[2]) == CONST_INT
8871       && GET_CODE (operands[3]) == CONST_INT)
8872     {
8873       rtx out = operands[0];
8874       HOST_WIDE_INT ct = INTVAL (operands[2]);
8875       HOST_WIDE_INT cf = INTVAL (operands[3]);
8876       HOST_WIDE_INT diff;
8877
8878       diff = ct - cf;
8879       /*  Sign bit compares are better done using shifts than we do by using
8880           sbb.  */
8881       if (sign_bit_compare_p
8882           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
8883                                              ix86_compare_op1, &compare_op))
8884         {
8885           /* Detect overlap between destination and compare sources.  */
8886           rtx tmp = out;
8887
8888           if (!sign_bit_compare_p)
8889             {
8890               bool fpcmp = false;
8891
8892               compare_code = GET_CODE (compare_op);
8893
8894               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
8895                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
8896                 {
8897                   fpcmp = true;
8898                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
8899                 }
8900
8901               /* To simplify rest of code, restrict to the GEU case.  */
8902               if (compare_code == LTU)
8903                 {
8904                   HOST_WIDE_INT tmp = ct;
8905                   ct = cf;
8906                   cf = tmp;
8907                   compare_code = reverse_condition (compare_code);
8908                   code = reverse_condition (code);
8909                 }
8910               else
8911                 {
8912                   if (fpcmp)
8913                     PUT_CODE (compare_op,
8914                               reverse_condition_maybe_unordered
8915                                 (GET_CODE (compare_op)));
8916                   else
8917                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
8918                 }
8919               diff = ct - cf;
8920
8921               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
8922                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
8923                 tmp = gen_reg_rtx (mode);
8924
8925               if (mode == DImode)
8926                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
8927               else
8928                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
8929             }
8930           else
8931             {
8932               if (code == GT || code == GE)
8933                 code = reverse_condition (code);
8934               else
8935                 {
8936                   HOST_WIDE_INT tmp = ct;
8937                   ct = cf;
8938                   cf = tmp;
8939                   diff = ct - cf;
8940                 }
8941               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
8942                                      ix86_compare_op1, VOIDmode, 0, -1);
8943             }
8944
8945           if (diff == 1)
8946             {
8947               /*
8948                * cmpl op0,op1
8949                * sbbl dest,dest
8950                * [addl dest, ct]
8951                *
8952                * Size 5 - 8.
8953                */
8954               if (ct)
8955                 tmp = expand_simple_binop (mode, PLUS,
8956                                            tmp, GEN_INT (ct),
8957                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
8958             }
8959           else if (cf == -1)
8960             {
8961               /*
8962                * cmpl op0,op1
8963                * sbbl dest,dest
8964                * orl $ct, dest
8965                *
8966                * Size 8.
8967                */
8968               tmp = expand_simple_binop (mode, IOR,
8969                                          tmp, GEN_INT (ct),
8970                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
8971             }
8972           else if (diff == -1 && ct)
8973             {
8974               /*
8975                * cmpl op0,op1
8976                * sbbl dest,dest
8977                * notl dest
8978                * [addl dest, cf]
8979                *
8980                * Size 8 - 11.
8981                */
8982               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
8983               if (cf)
8984                 tmp = expand_simple_binop (mode, PLUS,
8985                                            copy_rtx (tmp), GEN_INT (cf),
8986                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
8987             }
8988           else
8989             {
8990               /*
8991                * cmpl op0,op1
8992                * sbbl dest,dest
8993                * [notl dest]
8994                * andl cf - ct, dest
8995                * [addl dest, ct]
8996                *
8997                * Size 8 - 11.
8998                */
8999
9000               if (cf == 0)
9001                 {
9002                   cf = ct;
9003                   ct = 0;
9004                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
9005                 }
9006
9007               tmp = expand_simple_binop (mode, AND,
9008                                          copy_rtx (tmp),
9009                                          gen_int_mode (cf - ct, mode),
9010                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
9011               if (ct)
9012                 tmp = expand_simple_binop (mode, PLUS,
9013                                            copy_rtx (tmp), GEN_INT (ct),
9014                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9015             }
9016
9017           if (!rtx_equal_p (tmp, out))
9018             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
9019
9020           return 1; /* DONE */
9021         }
9022
9023       if (diff < 0)
9024         {
9025           HOST_WIDE_INT tmp;
9026           tmp = ct, ct = cf, cf = tmp;
9027           diff = -diff;
9028           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9029             {
9030               /* We may be reversing unordered compare to normal compare, that
9031                  is not valid in general (we may convert non-trapping condition
9032                  to trapping one), however on i386 we currently emit all
9033                  comparisons unordered.  */
9034               compare_code = reverse_condition_maybe_unordered (compare_code);
9035               code = reverse_condition_maybe_unordered (code);
9036             }
9037           else
9038             {
9039               compare_code = reverse_condition (compare_code);
9040               code = reverse_condition (code);
9041             }
9042         }
9043
9044       compare_code = UNKNOWN;
9045       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
9046           && GET_CODE (ix86_compare_op1) == CONST_INT)
9047         {
9048           if (ix86_compare_op1 == const0_rtx
9049               && (code == LT || code == GE))
9050             compare_code = code;
9051           else if (ix86_compare_op1 == constm1_rtx)
9052             {
9053               if (code == LE)
9054                 compare_code = LT;
9055               else if (code == GT)
9056                 compare_code = GE;
9057             }
9058         }
9059
9060       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
9061       if (compare_code != UNKNOWN
9062           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
9063           && (cf == -1 || ct == -1))
9064         {
9065           /* If lea code below could be used, only optimize
9066              if it results in a 2 insn sequence.  */
9067
9068           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
9069                  || diff == 3 || diff == 5 || diff == 9)
9070               || (compare_code == LT && ct == -1)
9071               || (compare_code == GE && cf == -1))
9072             {
9073               /*
9074                * notl op1       (if necessary)
9075                * sarl $31, op1
9076                * orl cf, op1
9077                */
9078               if (ct != -1)
9079                 {
9080                   cf = ct;
9081                   ct = -1;
9082                   code = reverse_condition (code);
9083                 }
9084
9085               out = emit_store_flag (out, code, ix86_compare_op0,
9086                                      ix86_compare_op1, VOIDmode, 0, -1);
9087
9088               out = expand_simple_binop (mode, IOR,
9089                                          out, GEN_INT (cf),
9090                                          out, 1, OPTAB_DIRECT);
9091               if (out != operands[0])
9092                 emit_move_insn (operands[0], out);
9093
9094               return 1; /* DONE */
9095             }
9096         }
9097
9098
9099       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
9100            || diff == 3 || diff == 5 || diff == 9)
9101           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
9102           && (mode != DImode
9103               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
9104         {
9105           /*
9106            * xorl dest,dest
9107            * cmpl op1,op2
9108            * setcc dest
9109            * lea cf(dest*(ct-cf)),dest
9110            *
9111            * Size 14.
9112            *
9113            * This also catches the degenerate setcc-only case.
9114            */
9115
9116           rtx tmp;
9117           int nops;
9118
9119           out = emit_store_flag (out, code, ix86_compare_op0,
9120                                  ix86_compare_op1, VOIDmode, 0, 1);
9121
9122           nops = 0;
9123           /* On x86_64 the lea instruction operates on Pmode, so we need
9124              to get arithmetics done in proper mode to match.  */
9125           if (diff == 1)
9126             tmp = copy_rtx (out);
9127           else
9128             {
9129               rtx out1;
9130               out1 = copy_rtx (out);
9131               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
9132               nops++;
9133               if (diff & 1)
9134                 {
9135                   tmp = gen_rtx_PLUS (mode, tmp, out1);
9136                   nops++;
9137                 }
9138             }
9139           if (cf != 0)
9140             {
9141               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
9142               nops++;
9143             }
9144           if (!rtx_equal_p (tmp, out))
9145             {
9146               if (nops == 1)
9147                 out = force_operand (tmp, copy_rtx (out));
9148               else
9149                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
9150             }
9151           if (!rtx_equal_p (out, operands[0]))
9152             emit_move_insn (operands[0], copy_rtx (out));
9153
9154           return 1; /* DONE */
9155         }
9156
9157       /*
9158        * General case:                  Jumpful:
9159        *   xorl dest,dest               cmpl op1, op2
9160        *   cmpl op1, op2                movl ct, dest
9161        *   setcc dest                   jcc 1f
9162        *   decl dest                    movl cf, dest
9163        *   andl (cf-ct),dest            1:
9164        *   addl ct,dest
9165        *
9166        * Size 20.                       Size 14.
9167        *
9168        * This is reasonably steep, but branch mispredict costs are
9169        * high on modern cpus, so consider failing only if optimizing
9170        * for space.
9171        */
9172
9173       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
9174           && BRANCH_COST >= 2)
9175         {
9176           if (cf == 0)
9177             {
9178               cf = ct;
9179               ct = 0;
9180               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9181                 /* We may be reversing unordered compare to normal compare,
9182                    that is not valid in general (we may convert non-trapping
9183                    condition to trapping one), however on i386 we currently
9184                    emit all comparisons unordered.  */
9185                 code = reverse_condition_maybe_unordered (code);
9186               else
9187                 {
9188                   code = reverse_condition (code);
9189                   if (compare_code != UNKNOWN)
9190                     compare_code = reverse_condition (compare_code);
9191                 }
9192             }
9193
9194           if (compare_code != UNKNOWN)
9195             {
9196               /* notl op1       (if needed)
9197                  sarl $31, op1
9198                  andl (cf-ct), op1
9199                  addl ct, op1
9200
9201                  For x < 0 (resp. x <= -1) there will be no notl,
9202                  so if possible swap the constants to get rid of the
9203                  complement.
9204                  True/false will be -1/0 while code below (store flag
9205                  followed by decrement) is 0/-1, so the constants need
9206                  to be exchanged once more.  */
9207
9208               if (compare_code == GE || !cf)
9209                 {
9210                   code = reverse_condition (code);
9211                   compare_code = LT;
9212                 }
9213               else
9214                 {
9215                   HOST_WIDE_INT tmp = cf;
9216                   cf = ct;
9217                   ct = tmp;
9218                 }
9219
9220               out = emit_store_flag (out, code, ix86_compare_op0,
9221                                      ix86_compare_op1, VOIDmode, 0, -1);
9222             }
9223           else
9224             {
9225               out = emit_store_flag (out, code, ix86_compare_op0,
9226                                      ix86_compare_op1, VOIDmode, 0, 1);
9227
9228               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
9229                                          copy_rtx (out), 1, OPTAB_DIRECT);
9230             }
9231
9232           out = expand_simple_binop (mode, AND, copy_rtx (out),
9233                                      gen_int_mode (cf - ct, mode),
9234                                      copy_rtx (out), 1, OPTAB_DIRECT);
9235           if (ct)
9236             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
9237                                        copy_rtx (out), 1, OPTAB_DIRECT);
9238           if (!rtx_equal_p (out, operands[0]))
9239             emit_move_insn (operands[0], copy_rtx (out));
9240
9241           return 1; /* DONE */
9242         }
9243     }
9244
9245   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
9246     {
9247       /* Try a few things more with specific constants and a variable.  */
9248
9249       optab op;
9250       rtx var, orig_out, out, tmp;
9251
9252       if (BRANCH_COST <= 2)
9253         return 0; /* FAIL */
9254
9255       /* If one of the two operands is an interesting constant, load a
9256          constant with the above and mask it in with a logical operation.  */
9257
9258       if (GET_CODE (operands[2]) == CONST_INT)
9259         {
9260           var = operands[3];
9261           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
9262             operands[3] = constm1_rtx, op = and_optab;
9263           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
9264             operands[3] = const0_rtx, op = ior_optab;
9265           else
9266             return 0; /* FAIL */
9267         }
9268       else if (GET_CODE (operands[3]) == CONST_INT)
9269         {
9270           var = operands[2];
9271           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
9272             operands[2] = constm1_rtx, op = and_optab;
9273           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
9274             operands[2] = const0_rtx, op = ior_optab;
9275           else
9276             return 0; /* FAIL */
9277         }
9278       else
9279         return 0; /* FAIL */
9280
9281       orig_out = operands[0];
9282       tmp = gen_reg_rtx (mode);
9283       operands[0] = tmp;
9284
9285       /* Recurse to get the constant loaded.  */
9286       if (ix86_expand_int_movcc (operands) == 0)
9287         return 0; /* FAIL */
9288
9289       /* Mask in the interesting variable.  */
9290       out = expand_binop (mode, op, var, tmp, orig_out, 0,
9291                           OPTAB_WIDEN);
9292       if (!rtx_equal_p (out, orig_out))
9293         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
9294
9295       return 1; /* DONE */
9296     }
9297
9298   /*
9299    * For comparison with above,
9300    *
9301    * movl cf,dest
9302    * movl ct,tmp
9303    * cmpl op1,op2
9304    * cmovcc tmp,dest
9305    *
9306    * Size 15.
9307    */
9308
9309   if (! nonimmediate_operand (operands[2], mode))
9310     operands[2] = force_reg (mode, operands[2]);
9311   if (! nonimmediate_operand (operands[3], mode))
9312     operands[3] = force_reg (mode, operands[3]);
9313
9314   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
9315     {
9316       rtx tmp = gen_reg_rtx (mode);
9317       emit_move_insn (tmp, operands[3]);
9318       operands[3] = tmp;
9319     }
9320   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
9321     {
9322       rtx tmp = gen_reg_rtx (mode);
9323       emit_move_insn (tmp, operands[2]);
9324       operands[2] = tmp;
9325     }
9326
9327   if (! register_operand (operands[2], VOIDmode)
9328       && (mode == QImode
9329           || ! register_operand (operands[3], VOIDmode)))
9330     operands[2] = force_reg (mode, operands[2]);
9331
9332   if (mode == QImode
9333       && ! register_operand (operands[3], VOIDmode))
9334     operands[3] = force_reg (mode, operands[3]);
9335
9336   emit_insn (compare_seq);
9337   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
9338                           gen_rtx_IF_THEN_ELSE (mode,
9339                                                 compare_op, operands[2],
9340                                                 operands[3])));
9341   if (bypass_test)
9342     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
9343                             gen_rtx_IF_THEN_ELSE (mode,
9344                                   bypass_test,
9345                                   copy_rtx (operands[3]),
9346                                   copy_rtx (operands[0]))));
9347   if (second_test)
9348     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
9349                             gen_rtx_IF_THEN_ELSE (mode,
9350                                   second_test,
9351                                   copy_rtx (operands[2]),
9352                                   copy_rtx (operands[0]))));
9353
9354   return 1; /* DONE */
9355 }
9356
9357 int
9358 ix86_expand_fp_movcc (rtx operands[])
9359 {
9360   enum rtx_code code;
9361   rtx tmp;
9362   rtx compare_op, second_test, bypass_test;
9363
9364   /* For SF/DFmode conditional moves based on comparisons
9365      in same mode, we may want to use SSE min/max instructions.  */
9366   if (((TARGET_SSE_MATH && GET_MODE (operands[0]) == SFmode)
9367        || (TARGET_SSE2 && TARGET_SSE_MATH && GET_MODE (operands[0]) == DFmode))
9368       && GET_MODE (ix86_compare_op0) == GET_MODE (operands[0])
9369       /* The SSE comparisons does not support the LTGT/UNEQ pair.  */
9370       && (!TARGET_IEEE_FP
9371           || (GET_CODE (operands[1]) != LTGT && GET_CODE (operands[1]) != UNEQ))
9372       /* We may be called from the post-reload splitter.  */
9373       && (!REG_P (operands[0])
9374           || SSE_REG_P (operands[0])
9375           || REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER))
9376     {
9377       rtx op0 = ix86_compare_op0, op1 = ix86_compare_op1;
9378       code = GET_CODE (operands[1]);
9379
9380       /* See if we have (cross) match between comparison operands and
9381          conditional move operands.  */
9382       if (rtx_equal_p (operands[2], op1))
9383         {
9384           rtx tmp = op0;
9385           op0 = op1;
9386           op1 = tmp;
9387           code = reverse_condition_maybe_unordered (code);
9388         }
9389       if (rtx_equal_p (operands[2], op0) && rtx_equal_p (operands[3], op1))
9390         {
9391           /* Check for min operation.  */
9392           if (code == LT || code == UNLE)
9393             {
9394                if (code == UNLE)
9395                 {
9396                   rtx tmp = op0;
9397                   op0 = op1;
9398                   op1 = tmp;
9399                 }
9400                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
9401                if (memory_operand (op0, VOIDmode))
9402                  op0 = force_reg (GET_MODE (operands[0]), op0);
9403                if (GET_MODE (operands[0]) == SFmode)
9404                  emit_insn (gen_minsf3 (operands[0], op0, op1));
9405                else
9406                  emit_insn (gen_mindf3 (operands[0], op0, op1));
9407                return 1;
9408             }
9409           /* Check for max operation.  */
9410           if (code == GT || code == UNGE)
9411             {
9412                if (code == UNGE)
9413                 {
9414                   rtx tmp = op0;
9415                   op0 = op1;
9416                   op1 = tmp;
9417                 }
9418                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
9419                if (memory_operand (op0, VOIDmode))
9420                  op0 = force_reg (GET_MODE (operands[0]), op0);
9421                if (GET_MODE (operands[0]) == SFmode)
9422                  emit_insn (gen_maxsf3 (operands[0], op0, op1));
9423                else
9424                  emit_insn (gen_maxdf3 (operands[0], op0, op1));
9425                return 1;
9426             }
9427         }
9428       /* Manage condition to be sse_comparison_operator.  In case we are
9429          in non-ieee mode, try to canonicalize the destination operand
9430          to be first in the comparison - this helps reload to avoid extra
9431          moves.  */
9432       if (!sse_comparison_operator (operands[1], VOIDmode)
9433           || (rtx_equal_p (operands[0], ix86_compare_op1) && !TARGET_IEEE_FP))
9434         {
9435           rtx tmp = ix86_compare_op0;
9436           ix86_compare_op0 = ix86_compare_op1;
9437           ix86_compare_op1 = tmp;
9438           operands[1] = gen_rtx_fmt_ee (swap_condition (GET_CODE (operands[1])),
9439                                         VOIDmode, ix86_compare_op0,
9440                                         ix86_compare_op1);
9441         }
9442       /* Similarly try to manage result to be first operand of conditional
9443          move. We also don't support the NE comparison on SSE, so try to
9444          avoid it.  */
9445       if ((rtx_equal_p (operands[0], operands[3])
9446            && (!TARGET_IEEE_FP || GET_CODE (operands[1]) != EQ))
9447           || (GET_CODE (operands[1]) == NE && TARGET_IEEE_FP))
9448         {
9449           rtx tmp = operands[2];
9450           operands[2] = operands[3];
9451           operands[3] = tmp;
9452           operands[1] = gen_rtx_fmt_ee (reverse_condition_maybe_unordered
9453                                           (GET_CODE (operands[1])),
9454                                         VOIDmode, ix86_compare_op0,
9455                                         ix86_compare_op1);
9456         }
9457       if (GET_MODE (operands[0]) == SFmode)
9458         emit_insn (gen_sse_movsfcc (operands[0], operands[1],
9459                                     operands[2], operands[3],
9460                                     ix86_compare_op0, ix86_compare_op1));
9461       else
9462         emit_insn (gen_sse_movdfcc (operands[0], operands[1],
9463                                     operands[2], operands[3],
9464                                     ix86_compare_op0, ix86_compare_op1));
9465       return 1;
9466     }
9467
9468   /* The floating point conditional move instructions don't directly
9469      support conditions resulting from a signed integer comparison.  */
9470
9471   code = GET_CODE (operands[1]);
9472   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
9473
9474   /* The floating point conditional move instructions don't directly
9475      support signed integer comparisons.  */
9476
9477   if (!fcmov_comparison_operator (compare_op, VOIDmode))
9478     {
9479       if (second_test != NULL || bypass_test != NULL)
9480         abort ();
9481       tmp = gen_reg_rtx (QImode);
9482       ix86_expand_setcc (code, tmp);
9483       code = NE;
9484       ix86_compare_op0 = tmp;
9485       ix86_compare_op1 = const0_rtx;
9486       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
9487     }
9488   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
9489     {
9490       tmp = gen_reg_rtx (GET_MODE (operands[0]));
9491       emit_move_insn (tmp, operands[3]);
9492       operands[3] = tmp;
9493     }
9494   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
9495     {
9496       tmp = gen_reg_rtx (GET_MODE (operands[0]));
9497       emit_move_insn (tmp, operands[2]);
9498       operands[2] = tmp;
9499     }
9500
9501   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
9502                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
9503                                 compare_op,
9504                                 operands[2],
9505                                 operands[3])));
9506   if (bypass_test)
9507     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
9508                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
9509                                   bypass_test,
9510                                   operands[3],
9511                                   operands[0])));
9512   if (second_test)
9513     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
9514                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
9515                                   second_test,
9516                                   operands[2],
9517                                   operands[0])));
9518
9519   return 1;
9520 }
9521
9522 /* Expand conditional increment or decrement using adb/sbb instructions.
9523    The default case using setcc followed by the conditional move can be
9524    done by generic code.  */
9525 int
9526 ix86_expand_int_addcc (rtx operands[])
9527 {
9528   enum rtx_code code = GET_CODE (operands[1]);
9529   rtx compare_op;
9530   rtx val = const0_rtx;
9531   bool fpcmp = false;
9532   enum machine_mode mode = GET_MODE (operands[0]);
9533
9534   if (operands[3] != const1_rtx
9535       && operands[3] != constm1_rtx)
9536     return 0;
9537   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
9538                                        ix86_compare_op1, &compare_op))
9539      return 0;
9540   code = GET_CODE (compare_op);
9541
9542   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
9543       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
9544     {
9545       fpcmp = true;
9546       code = ix86_fp_compare_code_to_integer (code);
9547     }
9548
9549   if (code != LTU)
9550     {
9551       val = constm1_rtx;
9552       if (fpcmp)
9553         PUT_CODE (compare_op,
9554                   reverse_condition_maybe_unordered
9555                     (GET_CODE (compare_op)));
9556       else
9557         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
9558     }
9559   PUT_MODE (compare_op, mode);
9560
9561   /* Construct either adc or sbb insn.  */
9562   if ((code == LTU) == (operands[3] == constm1_rtx))
9563     {
9564       switch (GET_MODE (operands[0]))
9565         {
9566           case QImode:
9567             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
9568             break;
9569           case HImode:
9570             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
9571             break;
9572           case SImode:
9573             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
9574             break;
9575           case DImode:
9576             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
9577             break;
9578           default:
9579             abort ();
9580         }
9581     }
9582   else
9583     {
9584       switch (GET_MODE (operands[0]))
9585         {
9586           case QImode:
9587             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
9588             break;
9589           case HImode:
9590             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
9591             break;
9592           case SImode:
9593             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
9594             break;
9595           case DImode:
9596             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
9597             break;
9598           default:
9599             abort ();
9600         }
9601     }
9602   return 1; /* DONE */
9603 }
9604
9605
9606 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
9607    works for floating pointer parameters and nonoffsetable memories.
9608    For pushes, it returns just stack offsets; the values will be saved
9609    in the right order.  Maximally three parts are generated.  */
9610
9611 static int
9612 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
9613 {
9614   int size;
9615
9616   if (!TARGET_64BIT)
9617     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
9618   else
9619     size = (GET_MODE_SIZE (mode) + 4) / 8;
9620
9621   if (GET_CODE (operand) == REG && MMX_REGNO_P (REGNO (operand)))
9622     abort ();
9623   if (size < 2 || size > 3)
9624     abort ();
9625
9626   /* Optimize constant pool reference to immediates.  This is used by fp
9627      moves, that force all constants to memory to allow combining.  */
9628   if (GET_CODE (operand) == MEM && MEM_READONLY_P (operand))
9629     {
9630       rtx tmp = maybe_get_pool_constant (operand);
9631       if (tmp)
9632         operand = tmp;
9633     }
9634
9635   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
9636     {
9637       /* The only non-offsetable memories we handle are pushes.  */
9638       if (! push_operand (operand, VOIDmode))
9639         abort ();
9640
9641       operand = copy_rtx (operand);
9642       PUT_MODE (operand, Pmode);
9643       parts[0] = parts[1] = parts[2] = operand;
9644     }
9645   else if (!TARGET_64BIT)
9646     {
9647       if (mode == DImode)
9648         split_di (&operand, 1, &parts[0], &parts[1]);
9649       else
9650         {
9651           if (REG_P (operand))
9652             {
9653               if (!reload_completed)
9654                 abort ();
9655               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
9656               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
9657               if (size == 3)
9658                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
9659             }
9660           else if (offsettable_memref_p (operand))
9661             {
9662               operand = adjust_address (operand, SImode, 0);
9663               parts[0] = operand;
9664               parts[1] = adjust_address (operand, SImode, 4);
9665               if (size == 3)
9666                 parts[2] = adjust_address (operand, SImode, 8);
9667             }
9668           else if (GET_CODE (operand) == CONST_DOUBLE)
9669             {
9670               REAL_VALUE_TYPE r;
9671               long l[4];
9672
9673               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
9674               switch (mode)
9675                 {
9676                 case XFmode:
9677                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
9678                   parts[2] = gen_int_mode (l[2], SImode);
9679                   break;
9680                 case DFmode:
9681                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
9682                   break;
9683                 default:
9684                   abort ();
9685                 }
9686               parts[1] = gen_int_mode (l[1], SImode);
9687               parts[0] = gen_int_mode (l[0], SImode);
9688             }
9689           else
9690             abort ();
9691         }
9692     }
9693   else
9694     {
9695       if (mode == TImode)
9696         split_ti (&operand, 1, &parts[0], &parts[1]);
9697       if (mode == XFmode || mode == TFmode)
9698         {
9699           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
9700           if (REG_P (operand))
9701             {
9702               if (!reload_completed)
9703                 abort ();
9704               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
9705               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
9706             }
9707           else if (offsettable_memref_p (operand))
9708             {
9709               operand = adjust_address (operand, DImode, 0);
9710               parts[0] = operand;
9711               parts[1] = adjust_address (operand, upper_mode, 8);
9712             }
9713           else if (GET_CODE (operand) == CONST_DOUBLE)
9714             {
9715               REAL_VALUE_TYPE r;
9716               long l[3];
9717
9718               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
9719               real_to_target (l, &r, mode);
9720               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
9721               if (HOST_BITS_PER_WIDE_INT >= 64)
9722                 parts[0]
9723                   = gen_int_mode
9724                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
9725                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
9726                        DImode);
9727               else
9728                 parts[0] = immed_double_const (l[0], l[1], DImode);
9729               if (upper_mode == SImode)
9730                 parts[1] = gen_int_mode (l[2], SImode);
9731               else if (HOST_BITS_PER_WIDE_INT >= 64)
9732                 parts[1]
9733                   = gen_int_mode
9734                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
9735                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
9736                        DImode);
9737               else
9738                 parts[1] = immed_double_const (l[2], l[3], DImode);
9739             }
9740           else
9741             abort ();
9742         }
9743     }
9744
9745   return size;
9746 }
9747
9748 /* Emit insns to perform a move or push of DI, DF, and XF values.
9749    Return false when normal moves are needed; true when all required
9750    insns have been emitted.  Operands 2-4 contain the input values
9751    int the correct order; operands 5-7 contain the output values.  */
9752
9753 void
9754 ix86_split_long_move (rtx operands[])
9755 {
9756   rtx part[2][3];
9757   int nparts;
9758   int push = 0;
9759   int collisions = 0;
9760   enum machine_mode mode = GET_MODE (operands[0]);
9761
9762   /* The DFmode expanders may ask us to move double.
9763      For 64bit target this is single move.  By hiding the fact
9764      here we simplify i386.md splitters.  */
9765   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
9766     {
9767       /* Optimize constant pool reference to immediates.  This is used by
9768          fp moves, that force all constants to memory to allow combining.  */
9769
9770       if (GET_CODE (operands[1]) == MEM
9771           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
9772           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
9773         operands[1] = get_pool_constant (XEXP (operands[1], 0));
9774       if (push_operand (operands[0], VOIDmode))
9775         {
9776           operands[0] = copy_rtx (operands[0]);
9777           PUT_MODE (operands[0], Pmode);
9778         }
9779       else
9780         operands[0] = gen_lowpart (DImode, operands[0]);
9781       operands[1] = gen_lowpart (DImode, operands[1]);
9782       emit_move_insn (operands[0], operands[1]);
9783       return;
9784     }
9785
9786   /* The only non-offsettable memory we handle is push.  */
9787   if (push_operand (operands[0], VOIDmode))
9788     push = 1;
9789   else if (GET_CODE (operands[0]) == MEM
9790            && ! offsettable_memref_p (operands[0]))
9791     abort ();
9792
9793   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
9794   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
9795
9796   /* When emitting push, take care for source operands on the stack.  */
9797   if (push && GET_CODE (operands[1]) == MEM
9798       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
9799     {
9800       if (nparts == 3)
9801         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
9802                                      XEXP (part[1][2], 0));
9803       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
9804                                    XEXP (part[1][1], 0));
9805     }
9806
9807   /* We need to do copy in the right order in case an address register
9808      of the source overlaps the destination.  */
9809   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
9810     {
9811       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
9812         collisions++;
9813       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
9814         collisions++;
9815       if (nparts == 3
9816           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
9817         collisions++;
9818
9819       /* Collision in the middle part can be handled by reordering.  */
9820       if (collisions == 1 && nparts == 3
9821           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
9822         {
9823           rtx tmp;
9824           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
9825           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
9826         }
9827
9828       /* If there are more collisions, we can't handle it by reordering.
9829          Do an lea to the last part and use only one colliding move.  */
9830       else if (collisions > 1)
9831         {
9832           rtx base;
9833
9834           collisions = 1;
9835
9836           base = part[0][nparts - 1];
9837
9838           /* Handle the case when the last part isn't valid for lea.
9839              Happens in 64-bit mode storing the 12-byte XFmode.  */
9840           if (GET_MODE (base) != Pmode)
9841             base = gen_rtx_REG (Pmode, REGNO (base));
9842
9843           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
9844           part[1][0] = replace_equiv_address (part[1][0], base);
9845           part[1][1] = replace_equiv_address (part[1][1],
9846                                       plus_constant (base, UNITS_PER_WORD));
9847           if (nparts == 3)
9848             part[1][2] = replace_equiv_address (part[1][2],
9849                                       plus_constant (base, 8));
9850         }
9851     }
9852
9853   if (push)
9854     {
9855       if (!TARGET_64BIT)
9856         {
9857           if (nparts == 3)
9858             {
9859               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
9860                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
9861               emit_move_insn (part[0][2], part[1][2]);
9862             }
9863         }
9864       else
9865         {
9866           /* In 64bit mode we don't have 32bit push available.  In case this is
9867              register, it is OK - we will just use larger counterpart.  We also
9868              retype memory - these comes from attempt to avoid REX prefix on
9869              moving of second half of TFmode value.  */
9870           if (GET_MODE (part[1][1]) == SImode)
9871             {
9872               if (GET_CODE (part[1][1]) == MEM)
9873                 part[1][1] = adjust_address (part[1][1], DImode, 0);
9874               else if (REG_P (part[1][1]))
9875                 part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
9876               else
9877                 abort ();
9878               if (GET_MODE (part[1][0]) == SImode)
9879                 part[1][0] = part[1][1];
9880             }
9881         }
9882       emit_move_insn (part[0][1], part[1][1]);
9883       emit_move_insn (part[0][0], part[1][0]);
9884       return;
9885     }
9886
9887   /* Choose correct order to not overwrite the source before it is copied.  */
9888   if ((REG_P (part[0][0])
9889        && REG_P (part[1][1])
9890        && (REGNO (part[0][0]) == REGNO (part[1][1])
9891            || (nparts == 3
9892                && REGNO (part[0][0]) == REGNO (part[1][2]))))
9893       || (collisions > 0
9894           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
9895     {
9896       if (nparts == 3)
9897         {
9898           operands[2] = part[0][2];
9899           operands[3] = part[0][1];
9900           operands[4] = part[0][0];
9901           operands[5] = part[1][2];
9902           operands[6] = part[1][1];
9903           operands[7] = part[1][0];
9904         }
9905       else
9906         {
9907           operands[2] = part[0][1];
9908           operands[3] = part[0][0];
9909           operands[5] = part[1][1];
9910           operands[6] = part[1][0];
9911         }
9912     }
9913   else
9914     {
9915       if (nparts == 3)
9916         {
9917           operands[2] = part[0][0];
9918           operands[3] = part[0][1];
9919           operands[4] = part[0][2];
9920           operands[5] = part[1][0];
9921           operands[6] = part[1][1];
9922           operands[7] = part[1][2];
9923         }
9924       else
9925         {
9926           operands[2] = part[0][0];
9927           operands[3] = part[0][1];
9928           operands[5] = part[1][0];
9929           operands[6] = part[1][1];
9930         }
9931     }
9932   emit_move_insn (operands[2], operands[5]);
9933   emit_move_insn (operands[3], operands[6]);
9934   if (nparts == 3)
9935     emit_move_insn (operands[4], operands[7]);
9936
9937   return;
9938 }
9939
9940 void
9941 ix86_split_ashldi (rtx *operands, rtx scratch)
9942 {
9943   rtx low[2], high[2];
9944   int count;
9945
9946   if (GET_CODE (operands[2]) == CONST_INT)
9947     {
9948       split_di (operands, 2, low, high);
9949       count = INTVAL (operands[2]) & 63;
9950
9951       if (count >= 32)
9952         {
9953           emit_move_insn (high[0], low[1]);
9954           emit_move_insn (low[0], const0_rtx);
9955
9956           if (count > 32)
9957             emit_insn (gen_ashlsi3 (high[0], high[0], GEN_INT (count - 32)));
9958         }
9959       else
9960         {
9961           if (!rtx_equal_p (operands[0], operands[1]))
9962             emit_move_insn (operands[0], operands[1]);
9963           emit_insn (gen_x86_shld_1 (high[0], low[0], GEN_INT (count)));
9964           emit_insn (gen_ashlsi3 (low[0], low[0], GEN_INT (count)));
9965         }
9966     }
9967   else
9968     {
9969       if (!rtx_equal_p (operands[0], operands[1]))
9970         emit_move_insn (operands[0], operands[1]);
9971
9972       split_di (operands, 1, low, high);
9973
9974       emit_insn (gen_x86_shld_1 (high[0], low[0], operands[2]));
9975       emit_insn (gen_ashlsi3 (low[0], low[0], operands[2]));
9976
9977       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
9978         {
9979           if (! no_new_pseudos)
9980             scratch = force_reg (SImode, const0_rtx);
9981           else
9982             emit_move_insn (scratch, const0_rtx);
9983
9984           emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2],
9985                                           scratch));
9986         }
9987       else
9988         emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
9989     }
9990 }
9991
9992 void
9993 ix86_split_ashrdi (rtx *operands, rtx scratch)
9994 {
9995   rtx low[2], high[2];
9996   int count;
9997
9998   if (GET_CODE (operands[2]) == CONST_INT)
9999     {
10000       split_di (operands, 2, low, high);
10001       count = INTVAL (operands[2]) & 63;
10002
10003       if (count == 63)
10004         {
10005           emit_move_insn (high[0], high[1]);
10006           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (31)));
10007           emit_move_insn (low[0], high[0]);
10008
10009         }
10010       else if (count >= 32)
10011         {
10012           emit_move_insn (low[0], high[1]);
10013
10014           if (! reload_completed)
10015             emit_insn (gen_ashrsi3 (high[0], low[0], GEN_INT (31)));
10016           else
10017             {
10018               emit_move_insn (high[0], low[0]);
10019               emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (31)));
10020             }
10021
10022           if (count > 32)
10023             emit_insn (gen_ashrsi3 (low[0], low[0], GEN_INT (count - 32)));
10024         }
10025       else
10026         {
10027           if (!rtx_equal_p (operands[0], operands[1]))
10028             emit_move_insn (operands[0], operands[1]);
10029           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
10030           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (count)));
10031         }
10032     }
10033   else
10034     {
10035       if (!rtx_equal_p (operands[0], operands[1]))
10036         emit_move_insn (operands[0], operands[1]);
10037
10038       split_di (operands, 1, low, high);
10039
10040       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
10041       emit_insn (gen_ashrsi3 (high[0], high[0], operands[2]));
10042
10043       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10044         {
10045           if (! no_new_pseudos)
10046             scratch = gen_reg_rtx (SImode);
10047           emit_move_insn (scratch, high[0]);
10048           emit_insn (gen_ashrsi3 (scratch, scratch, GEN_INT (31)));
10049           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
10050                                           scratch));
10051         }
10052       else
10053         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
10054     }
10055 }
10056
10057 void
10058 ix86_split_lshrdi (rtx *operands, rtx scratch)
10059 {
10060   rtx low[2], high[2];
10061   int count;
10062
10063   if (GET_CODE (operands[2]) == CONST_INT)
10064     {
10065       split_di (operands, 2, low, high);
10066       count = INTVAL (operands[2]) & 63;
10067
10068       if (count >= 32)
10069         {
10070           emit_move_insn (low[0], high[1]);
10071           emit_move_insn (high[0], const0_rtx);
10072
10073           if (count > 32)
10074             emit_insn (gen_lshrsi3 (low[0], low[0], GEN_INT (count - 32)));
10075         }
10076       else
10077         {
10078           if (!rtx_equal_p (operands[0], operands[1]))
10079             emit_move_insn (operands[0], operands[1]);
10080           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
10081           emit_insn (gen_lshrsi3 (high[0], high[0], GEN_INT (count)));
10082         }
10083     }
10084   else
10085     {
10086       if (!rtx_equal_p (operands[0], operands[1]))
10087         emit_move_insn (operands[0], operands[1]);
10088
10089       split_di (operands, 1, low, high);
10090
10091       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
10092       emit_insn (gen_lshrsi3 (high[0], high[0], operands[2]));
10093
10094       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
10095       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10096         {
10097           if (! no_new_pseudos)
10098             scratch = force_reg (SImode, const0_rtx);
10099           else
10100             emit_move_insn (scratch, const0_rtx);
10101
10102           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
10103                                           scratch));
10104         }
10105       else
10106         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
10107     }
10108 }
10109
10110 /* Helper function for the string operations below.  Dest VARIABLE whether
10111    it is aligned to VALUE bytes.  If true, jump to the label.  */
10112 static rtx
10113 ix86_expand_aligntest (rtx variable, int value)
10114 {
10115   rtx label = gen_label_rtx ();
10116   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
10117   if (GET_MODE (variable) == DImode)
10118     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
10119   else
10120     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
10121   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
10122                            1, label);
10123   return label;
10124 }
10125
10126 /* Adjust COUNTER by the VALUE.  */
10127 static void
10128 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
10129 {
10130   if (GET_MODE (countreg) == DImode)
10131     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
10132   else
10133     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
10134 }
10135
10136 /* Zero extend possibly SImode EXP to Pmode register.  */
10137 rtx
10138 ix86_zero_extend_to_Pmode (rtx exp)
10139 {
10140   rtx r;
10141   if (GET_MODE (exp) == VOIDmode)
10142     return force_reg (Pmode, exp);
10143   if (GET_MODE (exp) == Pmode)
10144     return copy_to_mode_reg (Pmode, exp);
10145   r = gen_reg_rtx (Pmode);
10146   emit_insn (gen_zero_extendsidi2 (r, exp));
10147   return r;
10148 }
10149
10150 /* Expand string move (memcpy) operation.  Use i386 string operations when
10151    profitable.  expand_clrmem contains similar code.  */
10152 int
10153 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
10154 {
10155   rtx srcreg, destreg, countreg, srcexp, destexp;
10156   enum machine_mode counter_mode;
10157   HOST_WIDE_INT align = 0;
10158   unsigned HOST_WIDE_INT count = 0;
10159
10160   if (GET_CODE (align_exp) == CONST_INT)
10161     align = INTVAL (align_exp);
10162
10163   /* Can't use any of this if the user has appropriated esi or edi.  */
10164   if (global_regs[4] || global_regs[5])
10165     return 0;
10166
10167   /* This simple hack avoids all inlining code and simplifies code below.  */
10168   if (!TARGET_ALIGN_STRINGOPS)
10169     align = 64;
10170
10171   if (GET_CODE (count_exp) == CONST_INT)
10172     {
10173       count = INTVAL (count_exp);
10174       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
10175         return 0;
10176     }
10177
10178   /* Figure out proper mode for counter.  For 32bits it is always SImode,
10179      for 64bits use SImode when possible, otherwise DImode.
10180      Set count to number of bytes copied when known at compile time.  */
10181   if (!TARGET_64BIT
10182       || GET_MODE (count_exp) == SImode
10183       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
10184     counter_mode = SImode;
10185   else
10186     counter_mode = DImode;
10187
10188   if (counter_mode != SImode && counter_mode != DImode)
10189     abort ();
10190
10191   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
10192   if (destreg != XEXP (dst, 0))
10193     dst = replace_equiv_address_nv (dst, destreg);
10194   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
10195   if (srcreg != XEXP (src, 0))
10196     src = replace_equiv_address_nv (src, srcreg);
10197
10198   /* When optimizing for size emit simple rep ; movsb instruction for
10199      counts not divisible by 4.  */
10200
10201   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
10202     {
10203       emit_insn (gen_cld ());
10204       countreg = ix86_zero_extend_to_Pmode (count_exp);
10205       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
10206       srcexp = gen_rtx_PLUS (Pmode, srcreg, countreg);
10207       emit_insn (gen_rep_mov (destreg, dst, srcreg, src, countreg,
10208                               destexp, srcexp));
10209     }
10210
10211   /* For constant aligned (or small unaligned) copies use rep movsl
10212      followed by code copying the rest.  For PentiumPro ensure 8 byte
10213      alignment to allow rep movsl acceleration.  */
10214
10215   else if (count != 0
10216            && (align >= 8
10217                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
10218                || optimize_size || count < (unsigned int) 64))
10219     {
10220       unsigned HOST_WIDE_INT offset = 0;
10221       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
10222       rtx srcmem, dstmem;
10223
10224       emit_insn (gen_cld ());
10225       if (count & ~(size - 1))
10226         {
10227           countreg = copy_to_mode_reg (counter_mode,
10228                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
10229                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
10230           countreg = ix86_zero_extend_to_Pmode (countreg);
10231
10232           destexp = gen_rtx_ASHIFT (Pmode, countreg,
10233                                     GEN_INT (size == 4 ? 2 : 3));
10234           srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
10235           destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
10236
10237           emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
10238                                   countreg, destexp, srcexp));
10239           offset = count & ~(size - 1);
10240         }
10241       if (size == 8 && (count & 0x04))
10242         {
10243           srcmem = adjust_automodify_address_nv (src, SImode, srcreg,
10244                                                  offset);
10245           dstmem = adjust_automodify_address_nv (dst, SImode, destreg,
10246                                                  offset);
10247           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10248           offset += 4;
10249         }
10250       if (count & 0x02)
10251         {
10252           srcmem = adjust_automodify_address_nv (src, HImode, srcreg,
10253                                                  offset);
10254           dstmem = adjust_automodify_address_nv (dst, HImode, destreg,
10255                                                  offset);
10256           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10257           offset += 2;
10258         }
10259       if (count & 0x01)
10260         {
10261           srcmem = adjust_automodify_address_nv (src, QImode, srcreg,
10262                                                  offset);
10263           dstmem = adjust_automodify_address_nv (dst, QImode, destreg,
10264                                                  offset);
10265           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10266         }
10267     }
10268   /* The generic code based on the glibc implementation:
10269      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
10270      allowing accelerated copying there)
10271      - copy the data using rep movsl
10272      - copy the rest.  */
10273   else
10274     {
10275       rtx countreg2;
10276       rtx label = NULL;
10277       rtx srcmem, dstmem;
10278       int desired_alignment = (TARGET_PENTIUMPRO
10279                                && (count == 0 || count >= (unsigned int) 260)
10280                                ? 8 : UNITS_PER_WORD);
10281       /* Get rid of MEM_OFFSETs, they won't be accurate.  */
10282       dst = change_address (dst, BLKmode, destreg);
10283       src = change_address (src, BLKmode, srcreg);
10284
10285       /* In case we don't know anything about the alignment, default to
10286          library version, since it is usually equally fast and result in
10287          shorter code.
10288
10289          Also emit call when we know that the count is large and call overhead
10290          will not be important.  */
10291       if (!TARGET_INLINE_ALL_STRINGOPS
10292           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
10293         return 0;
10294
10295       if (TARGET_SINGLE_STRINGOP)
10296         emit_insn (gen_cld ());
10297
10298       countreg2 = gen_reg_rtx (Pmode);
10299       countreg = copy_to_mode_reg (counter_mode, count_exp);
10300
10301       /* We don't use loops to align destination and to copy parts smaller
10302          than 4 bytes, because gcc is able to optimize such code better (in
10303          the case the destination or the count really is aligned, gcc is often
10304          able to predict the branches) and also it is friendlier to the
10305          hardware branch prediction.
10306
10307          Using loops is beneficial for generic case, because we can
10308          handle small counts using the loops.  Many CPUs (such as Athlon)
10309          have large REP prefix setup costs.
10310
10311          This is quite costly.  Maybe we can revisit this decision later or
10312          add some customizability to this code.  */
10313
10314       if (count == 0 && align < desired_alignment)
10315         {
10316           label = gen_label_rtx ();
10317           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
10318                                    LEU, 0, counter_mode, 1, label);
10319         }
10320       if (align <= 1)
10321         {
10322           rtx label = ix86_expand_aligntest (destreg, 1);
10323           srcmem = change_address (src, QImode, srcreg);
10324           dstmem = change_address (dst, QImode, destreg);
10325           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10326           ix86_adjust_counter (countreg, 1);
10327           emit_label (label);
10328           LABEL_NUSES (label) = 1;
10329         }
10330       if (align <= 2)
10331         {
10332           rtx label = ix86_expand_aligntest (destreg, 2);
10333           srcmem = change_address (src, HImode, srcreg);
10334           dstmem = change_address (dst, HImode, destreg);
10335           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10336           ix86_adjust_counter (countreg, 2);
10337           emit_label (label);
10338           LABEL_NUSES (label) = 1;
10339         }
10340       if (align <= 4 && desired_alignment > 4)
10341         {
10342           rtx label = ix86_expand_aligntest (destreg, 4);
10343           srcmem = change_address (src, SImode, srcreg);
10344           dstmem = change_address (dst, SImode, destreg);
10345           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10346           ix86_adjust_counter (countreg, 4);
10347           emit_label (label);
10348           LABEL_NUSES (label) = 1;
10349         }
10350
10351       if (label && desired_alignment > 4 && !TARGET_64BIT)
10352         {
10353           emit_label (label);
10354           LABEL_NUSES (label) = 1;
10355           label = NULL_RTX;
10356         }
10357       if (!TARGET_SINGLE_STRINGOP)
10358         emit_insn (gen_cld ());
10359       if (TARGET_64BIT)
10360         {
10361           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
10362                                   GEN_INT (3)));
10363           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
10364         }
10365       else
10366         {
10367           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
10368           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
10369         }
10370       srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
10371       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
10372       emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
10373                               countreg2, destexp, srcexp));
10374
10375       if (label)
10376         {
10377           emit_label (label);
10378           LABEL_NUSES (label) = 1;
10379         }
10380       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
10381         {
10382           srcmem = change_address (src, SImode, srcreg);
10383           dstmem = change_address (dst, SImode, destreg);
10384           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10385         }
10386       if ((align <= 4 || count == 0) && TARGET_64BIT)
10387         {
10388           rtx label = ix86_expand_aligntest (countreg, 4);
10389           srcmem = change_address (src, SImode, srcreg);
10390           dstmem = change_address (dst, SImode, destreg);
10391           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10392           emit_label (label);
10393           LABEL_NUSES (label) = 1;
10394         }
10395       if (align > 2 && count != 0 && (count & 2))
10396         {
10397           srcmem = change_address (src, HImode, srcreg);
10398           dstmem = change_address (dst, HImode, destreg);
10399           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10400         }
10401       if (align <= 2 || count == 0)
10402         {
10403           rtx label = ix86_expand_aligntest (countreg, 2);
10404           srcmem = change_address (src, HImode, srcreg);
10405           dstmem = change_address (dst, HImode, destreg);
10406           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10407           emit_label (label);
10408           LABEL_NUSES (label) = 1;
10409         }
10410       if (align > 1 && count != 0 && (count & 1))
10411         {
10412           srcmem = change_address (src, QImode, srcreg);
10413           dstmem = change_address (dst, QImode, destreg);
10414           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10415         }
10416       if (align <= 1 || count == 0)
10417         {
10418           rtx label = ix86_expand_aligntest (countreg, 1);
10419           srcmem = change_address (src, QImode, srcreg);
10420           dstmem = change_address (dst, QImode, destreg);
10421           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
10422           emit_label (label);
10423           LABEL_NUSES (label) = 1;
10424         }
10425     }
10426
10427   return 1;
10428 }
10429
10430 /* Expand string clear operation (bzero).  Use i386 string operations when
10431    profitable.  expand_movmem contains similar code.  */
10432 int
10433 ix86_expand_clrmem (rtx dst, rtx count_exp, rtx align_exp)
10434 {
10435   rtx destreg, zeroreg, countreg, destexp;
10436   enum machine_mode counter_mode;
10437   HOST_WIDE_INT align = 0;
10438   unsigned HOST_WIDE_INT count = 0;
10439
10440   if (GET_CODE (align_exp) == CONST_INT)
10441     align = INTVAL (align_exp);
10442
10443   /* Can't use any of this if the user has appropriated esi.  */
10444   if (global_regs[4])
10445     return 0;
10446
10447   /* This simple hack avoids all inlining code and simplifies code below.  */
10448   if (!TARGET_ALIGN_STRINGOPS)
10449     align = 32;
10450
10451   if (GET_CODE (count_exp) == CONST_INT)
10452     {
10453       count = INTVAL (count_exp);
10454       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
10455         return 0;
10456     }
10457   /* Figure out proper mode for counter.  For 32bits it is always SImode,
10458      for 64bits use SImode when possible, otherwise DImode.
10459      Set count to number of bytes copied when known at compile time.  */
10460   if (!TARGET_64BIT
10461       || GET_MODE (count_exp) == SImode
10462       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
10463     counter_mode = SImode;
10464   else
10465     counter_mode = DImode;
10466
10467   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
10468   if (destreg != XEXP (dst, 0))
10469     dst = replace_equiv_address_nv (dst, destreg);
10470
10471
10472   /* When optimizing for size emit simple rep ; movsb instruction for
10473      counts not divisible by 4.  The movl $N, %ecx; rep; stosb
10474      sequence is 7 bytes long, so if optimizing for size and count is
10475      small enough that some stosl, stosw and stosb instructions without
10476      rep are shorter, fall back into the next if.  */
10477
10478   if ((!optimize || optimize_size)
10479       && (count == 0
10480           || ((count & 0x03)
10481               && (!optimize_size || (count & 0x03) + (count >> 2) > 7))))
10482     {
10483       emit_insn (gen_cld ());
10484
10485       countreg = ix86_zero_extend_to_Pmode (count_exp);
10486       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
10487       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
10488       emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg, destexp));
10489     }
10490   else if (count != 0
10491            && (align >= 8
10492                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
10493                || optimize_size || count < (unsigned int) 64))
10494     {
10495       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
10496       unsigned HOST_WIDE_INT offset = 0;
10497
10498       emit_insn (gen_cld ());
10499
10500       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
10501       if (count & ~(size - 1))
10502         {
10503           unsigned HOST_WIDE_INT repcount;
10504           unsigned int max_nonrep;
10505
10506           repcount = count >> (size == 4 ? 2 : 3);
10507           if (!TARGET_64BIT)
10508             repcount &= 0x3fffffff;
10509
10510           /* movl $N, %ecx; rep; stosl is 7 bytes, while N x stosl is N bytes.
10511              movl $N, %ecx; rep; stosq is 8 bytes, while N x stosq is 2xN
10512              bytes.  In both cases the latter seems to be faster for small
10513              values of N.  */
10514           max_nonrep = size == 4 ? 7 : 4;
10515           if (!optimize_size)
10516             switch (ix86_tune)
10517               {
10518               case PROCESSOR_PENTIUM4:
10519               case PROCESSOR_NOCONA:
10520                 max_nonrep = 3;
10521                 break;
10522               default:
10523                 break;
10524               }
10525
10526           if (repcount <= max_nonrep)
10527             while (repcount-- > 0)
10528               {
10529                 rtx mem = adjust_automodify_address_nv (dst,
10530                                                         GET_MODE (zeroreg),
10531                                                         destreg, offset);
10532                 emit_insn (gen_strset (destreg, mem, zeroreg));
10533                 offset += size;
10534               }
10535           else
10536             {
10537               countreg = copy_to_mode_reg (counter_mode, GEN_INT (repcount));
10538               countreg = ix86_zero_extend_to_Pmode (countreg);
10539               destexp = gen_rtx_ASHIFT (Pmode, countreg,
10540                                         GEN_INT (size == 4 ? 2 : 3));
10541               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
10542               emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg,
10543                                        destexp));
10544               offset = count & ~(size - 1);
10545             }
10546         }
10547       if (size == 8 && (count & 0x04))
10548         {
10549           rtx mem = adjust_automodify_address_nv (dst, SImode, destreg,
10550                                                   offset);
10551           emit_insn (gen_strset (destreg, mem,
10552                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
10553           offset += 4;
10554         }
10555       if (count & 0x02)
10556         {
10557           rtx mem = adjust_automodify_address_nv (dst, HImode, destreg,
10558                                                   offset);
10559           emit_insn (gen_strset (destreg, mem,
10560                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
10561           offset += 2;
10562         }
10563       if (count & 0x01)
10564         {
10565           rtx mem = adjust_automodify_address_nv (dst, QImode, destreg,
10566                                                   offset);
10567           emit_insn (gen_strset (destreg, mem,
10568                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
10569         }
10570     }
10571   else
10572     {
10573       rtx countreg2;
10574       rtx label = NULL;
10575       /* Compute desired alignment of the string operation.  */
10576       int desired_alignment = (TARGET_PENTIUMPRO
10577                                && (count == 0 || count >= (unsigned int) 260)
10578                                ? 8 : UNITS_PER_WORD);
10579
10580       /* In case we don't know anything about the alignment, default to
10581          library version, since it is usually equally fast and result in
10582          shorter code.
10583
10584          Also emit call when we know that the count is large and call overhead
10585          will not be important.  */
10586       if (!TARGET_INLINE_ALL_STRINGOPS
10587           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
10588         return 0;
10589
10590       if (TARGET_SINGLE_STRINGOP)
10591         emit_insn (gen_cld ());
10592
10593       countreg2 = gen_reg_rtx (Pmode);
10594       countreg = copy_to_mode_reg (counter_mode, count_exp);
10595       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
10596       /* Get rid of MEM_OFFSET, it won't be accurate.  */
10597       dst = change_address (dst, BLKmode, destreg);
10598
10599       if (count == 0 && align < desired_alignment)
10600         {
10601           label = gen_label_rtx ();
10602           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
10603                                    LEU, 0, counter_mode, 1, label);
10604         }
10605       if (align <= 1)
10606         {
10607           rtx label = ix86_expand_aligntest (destreg, 1);
10608           emit_insn (gen_strset (destreg, dst,
10609                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
10610           ix86_adjust_counter (countreg, 1);
10611           emit_label (label);
10612           LABEL_NUSES (label) = 1;
10613         }
10614       if (align <= 2)
10615         {
10616           rtx label = ix86_expand_aligntest (destreg, 2);
10617           emit_insn (gen_strset (destreg, dst,
10618                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
10619           ix86_adjust_counter (countreg, 2);
10620           emit_label (label);
10621           LABEL_NUSES (label) = 1;
10622         }
10623       if (align <= 4 && desired_alignment > 4)
10624         {
10625           rtx label = ix86_expand_aligntest (destreg, 4);
10626           emit_insn (gen_strset (destreg, dst,
10627                                  (TARGET_64BIT
10628                                   ? gen_rtx_SUBREG (SImode, zeroreg, 0)
10629                                   : zeroreg)));
10630           ix86_adjust_counter (countreg, 4);
10631           emit_label (label);
10632           LABEL_NUSES (label) = 1;
10633         }
10634
10635       if (label && desired_alignment > 4 && !TARGET_64BIT)
10636         {
10637           emit_label (label);
10638           LABEL_NUSES (label) = 1;
10639           label = NULL_RTX;
10640         }
10641
10642       if (!TARGET_SINGLE_STRINGOP)
10643         emit_insn (gen_cld ());
10644       if (TARGET_64BIT)
10645         {
10646           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
10647                                   GEN_INT (3)));
10648           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
10649         }
10650       else
10651         {
10652           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
10653           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
10654         }
10655       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
10656       emit_insn (gen_rep_stos (destreg, countreg2, dst, zeroreg, destexp));
10657
10658       if (label)
10659         {
10660           emit_label (label);
10661           LABEL_NUSES (label) = 1;
10662         }
10663
10664       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
10665         emit_insn (gen_strset (destreg, dst,
10666                                gen_rtx_SUBREG (SImode, zeroreg, 0)));
10667       if (TARGET_64BIT && (align <= 4 || count == 0))
10668         {
10669           rtx label = ix86_expand_aligntest (countreg, 4);
10670           emit_insn (gen_strset (destreg, dst,
10671                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
10672           emit_label (label);
10673           LABEL_NUSES (label) = 1;
10674         }
10675       if (align > 2 && count != 0 && (count & 2))
10676         emit_insn (gen_strset (destreg, dst,
10677                                gen_rtx_SUBREG (HImode, zeroreg, 0)));
10678       if (align <= 2 || count == 0)
10679         {
10680           rtx label = ix86_expand_aligntest (countreg, 2);
10681           emit_insn (gen_strset (destreg, dst,
10682                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
10683           emit_label (label);
10684           LABEL_NUSES (label) = 1;
10685         }
10686       if (align > 1 && count != 0 && (count & 1))
10687         emit_insn (gen_strset (destreg, dst,
10688                                gen_rtx_SUBREG (QImode, zeroreg, 0)));
10689       if (align <= 1 || count == 0)
10690         {
10691           rtx label = ix86_expand_aligntest (countreg, 1);
10692           emit_insn (gen_strset (destreg, dst,
10693                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
10694           emit_label (label);
10695           LABEL_NUSES (label) = 1;
10696         }
10697     }
10698   return 1;
10699 }
10700
10701 /* Expand strlen.  */
10702 int
10703 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
10704 {
10705   rtx addr, scratch1, scratch2, scratch3, scratch4;
10706
10707   /* The generic case of strlen expander is long.  Avoid it's
10708      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
10709
10710   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
10711       && !TARGET_INLINE_ALL_STRINGOPS
10712       && !optimize_size
10713       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
10714     return 0;
10715
10716   addr = force_reg (Pmode, XEXP (src, 0));
10717   scratch1 = gen_reg_rtx (Pmode);
10718
10719   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
10720       && !optimize_size)
10721     {
10722       /* Well it seems that some optimizer does not combine a call like
10723          foo(strlen(bar), strlen(bar));
10724          when the move and the subtraction is done here.  It does calculate
10725          the length just once when these instructions are done inside of
10726          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
10727          often used and I use one fewer register for the lifetime of
10728          output_strlen_unroll() this is better.  */
10729
10730       emit_move_insn (out, addr);
10731
10732       ix86_expand_strlensi_unroll_1 (out, src, align);
10733
10734       /* strlensi_unroll_1 returns the address of the zero at the end of
10735          the string, like memchr(), so compute the length by subtracting
10736          the start address.  */
10737       if (TARGET_64BIT)
10738         emit_insn (gen_subdi3 (out, out, addr));
10739       else
10740         emit_insn (gen_subsi3 (out, out, addr));
10741     }
10742   else
10743     {
10744       rtx unspec;
10745       scratch2 = gen_reg_rtx (Pmode);
10746       scratch3 = gen_reg_rtx (Pmode);
10747       scratch4 = force_reg (Pmode, constm1_rtx);
10748
10749       emit_move_insn (scratch3, addr);
10750       eoschar = force_reg (QImode, eoschar);
10751
10752       emit_insn (gen_cld ());
10753       src = replace_equiv_address_nv (src, scratch3);
10754
10755       /* If .md starts supporting :P, this can be done in .md.  */
10756       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
10757                                                  scratch4), UNSPEC_SCAS);
10758       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
10759       if (TARGET_64BIT)
10760         {
10761           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
10762           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
10763         }
10764       else
10765         {
10766           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
10767           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
10768         }
10769     }
10770   return 1;
10771 }
10772
10773 /* Expand the appropriate insns for doing strlen if not just doing
10774    repnz; scasb
10775
10776    out = result, initialized with the start address
10777    align_rtx = alignment of the address.
10778    scratch = scratch register, initialized with the startaddress when
10779         not aligned, otherwise undefined
10780
10781    This is just the body. It needs the initializations mentioned above and
10782    some address computing at the end.  These things are done in i386.md.  */
10783
10784 static void
10785 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
10786 {
10787   int align;
10788   rtx tmp;
10789   rtx align_2_label = NULL_RTX;
10790   rtx align_3_label = NULL_RTX;
10791   rtx align_4_label = gen_label_rtx ();
10792   rtx end_0_label = gen_label_rtx ();
10793   rtx mem;
10794   rtx tmpreg = gen_reg_rtx (SImode);
10795   rtx scratch = gen_reg_rtx (SImode);
10796   rtx cmp;
10797
10798   align = 0;
10799   if (GET_CODE (align_rtx) == CONST_INT)
10800     align = INTVAL (align_rtx);
10801
10802   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
10803
10804   /* Is there a known alignment and is it less than 4?  */
10805   if (align < 4)
10806     {
10807       rtx scratch1 = gen_reg_rtx (Pmode);
10808       emit_move_insn (scratch1, out);
10809       /* Is there a known alignment and is it not 2? */
10810       if (align != 2)
10811         {
10812           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
10813           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
10814
10815           /* Leave just the 3 lower bits.  */
10816           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
10817                                     NULL_RTX, 0, OPTAB_WIDEN);
10818
10819           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
10820                                    Pmode, 1, align_4_label);
10821           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
10822                                    Pmode, 1, align_2_label);
10823           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
10824                                    Pmode, 1, align_3_label);
10825         }
10826       else
10827         {
10828           /* Since the alignment is 2, we have to check 2 or 0 bytes;
10829              check if is aligned to 4 - byte.  */
10830
10831           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
10832                                     NULL_RTX, 0, OPTAB_WIDEN);
10833
10834           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
10835                                    Pmode, 1, align_4_label);
10836         }
10837
10838       mem = change_address (src, QImode, out);
10839
10840       /* Now compare the bytes.  */
10841
10842       /* Compare the first n unaligned byte on a byte per byte basis.  */
10843       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
10844                                QImode, 1, end_0_label);
10845
10846       /* Increment the address.  */
10847       if (TARGET_64BIT)
10848         emit_insn (gen_adddi3 (out, out, const1_rtx));
10849       else
10850         emit_insn (gen_addsi3 (out, out, const1_rtx));
10851
10852       /* Not needed with an alignment of 2 */
10853       if (align != 2)
10854         {
10855           emit_label (align_2_label);
10856
10857           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
10858                                    end_0_label);
10859
10860           if (TARGET_64BIT)
10861             emit_insn (gen_adddi3 (out, out, const1_rtx));
10862           else
10863             emit_insn (gen_addsi3 (out, out, const1_rtx));
10864
10865           emit_label (align_3_label);
10866         }
10867
10868       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
10869                                end_0_label);
10870
10871       if (TARGET_64BIT)
10872         emit_insn (gen_adddi3 (out, out, const1_rtx));
10873       else
10874         emit_insn (gen_addsi3 (out, out, const1_rtx));
10875     }
10876
10877   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
10878      align this loop.  It gives only huge programs, but does not help to
10879      speed up.  */
10880   emit_label (align_4_label);
10881
10882   mem = change_address (src, SImode, out);
10883   emit_move_insn (scratch, mem);
10884   if (TARGET_64BIT)
10885     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
10886   else
10887     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
10888
10889   /* This formula yields a nonzero result iff one of the bytes is zero.
10890      This saves three branches inside loop and many cycles.  */
10891
10892   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
10893   emit_insn (gen_one_cmplsi2 (scratch, scratch));
10894   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
10895   emit_insn (gen_andsi3 (tmpreg, tmpreg,
10896                          gen_int_mode (0x80808080, SImode)));
10897   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
10898                            align_4_label);
10899
10900   if (TARGET_CMOVE)
10901     {
10902        rtx reg = gen_reg_rtx (SImode);
10903        rtx reg2 = gen_reg_rtx (Pmode);
10904        emit_move_insn (reg, tmpreg);
10905        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
10906
10907        /* If zero is not in the first two bytes, move two bytes forward.  */
10908        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
10909        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
10910        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
10911        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
10912                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
10913                                                      reg,
10914                                                      tmpreg)));
10915        /* Emit lea manually to avoid clobbering of flags.  */
10916        emit_insn (gen_rtx_SET (SImode, reg2,
10917                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
10918
10919        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
10920        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
10921        emit_insn (gen_rtx_SET (VOIDmode, out,
10922                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
10923                                                      reg2,
10924                                                      out)));
10925
10926     }
10927   else
10928     {
10929        rtx end_2_label = gen_label_rtx ();
10930        /* Is zero in the first two bytes? */
10931
10932        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
10933        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
10934        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
10935        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10936                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
10937                             pc_rtx);
10938        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
10939        JUMP_LABEL (tmp) = end_2_label;
10940
10941        /* Not in the first two.  Move two bytes forward.  */
10942        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
10943        if (TARGET_64BIT)
10944          emit_insn (gen_adddi3 (out, out, const2_rtx));
10945        else
10946          emit_insn (gen_addsi3 (out, out, const2_rtx));
10947
10948        emit_label (end_2_label);
10949
10950     }
10951
10952   /* Avoid branch in fixing the byte.  */
10953   tmpreg = gen_lowpart (QImode, tmpreg);
10954   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
10955   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
10956   if (TARGET_64BIT)
10957     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
10958   else
10959     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
10960
10961   emit_label (end_0_label);
10962 }
10963
10964 void
10965 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
10966                   rtx callarg2 ATTRIBUTE_UNUSED,
10967                   rtx pop, int sibcall)
10968 {
10969   rtx use = NULL, call;
10970
10971   if (pop == const0_rtx)
10972     pop = NULL;
10973   if (TARGET_64BIT && pop)
10974     abort ();
10975
10976 #if TARGET_MACHO
10977   if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
10978     fnaddr = machopic_indirect_call_target (fnaddr);
10979 #else
10980   /* Static functions and indirect calls don't need the pic register.  */
10981   if (! TARGET_64BIT && flag_pic
10982       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
10983       && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
10984     use_reg (&use, pic_offset_table_rtx);
10985
10986   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
10987     {
10988       rtx al = gen_rtx_REG (QImode, 0);
10989       emit_move_insn (al, callarg2);
10990       use_reg (&use, al);
10991     }
10992 #endif /* TARGET_MACHO */
10993
10994   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
10995     {
10996       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
10997       fnaddr = gen_rtx_MEM (QImode, fnaddr);
10998     }
10999   if (sibcall && TARGET_64BIT
11000       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
11001     {
11002       rtx addr;
11003       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
11004       fnaddr = gen_rtx_REG (Pmode, FIRST_REX_INT_REG + 3 /* R11 */);
11005       emit_move_insn (fnaddr, addr);
11006       fnaddr = gen_rtx_MEM (QImode, fnaddr);
11007     }
11008
11009   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
11010   if (retval)
11011     call = gen_rtx_SET (VOIDmode, retval, call);
11012   if (pop)
11013     {
11014       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
11015       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
11016       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
11017     }
11018
11019   call = emit_call_insn (call);
11020   if (use)
11021     CALL_INSN_FUNCTION_USAGE (call) = use;
11022 }
11023
11024 \f
11025 /* Clear stack slot assignments remembered from previous functions.
11026    This is called from INIT_EXPANDERS once before RTL is emitted for each
11027    function.  */
11028
11029 static struct machine_function *
11030 ix86_init_machine_status (void)
11031 {
11032   struct machine_function *f;
11033
11034   f = ggc_alloc_cleared (sizeof (struct machine_function));
11035   f->use_fast_prologue_epilogue_nregs = -1;
11036
11037   return f;
11038 }
11039
11040 /* Return a MEM corresponding to a stack slot with mode MODE.
11041    Allocate a new slot if necessary.
11042
11043    The RTL for a function can have several slots available: N is
11044    which slot to use.  */
11045
11046 rtx
11047 assign_386_stack_local (enum machine_mode mode, int n)
11048 {
11049   struct stack_local_entry *s;
11050
11051   if (n < 0 || n >= MAX_386_STACK_LOCALS)
11052     abort ();
11053
11054   for (s = ix86_stack_locals; s; s = s->next)
11055     if (s->mode == mode && s->n == n)
11056       return s->rtl;
11057
11058   s = (struct stack_local_entry *)
11059     ggc_alloc (sizeof (struct stack_local_entry));
11060   s->n = n;
11061   s->mode = mode;
11062   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
11063
11064   s->next = ix86_stack_locals;
11065   ix86_stack_locals = s;
11066   return s->rtl;
11067 }
11068
11069 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
11070
11071 static GTY(()) rtx ix86_tls_symbol;
11072 rtx
11073 ix86_tls_get_addr (void)
11074 {
11075
11076   if (!ix86_tls_symbol)
11077     {
11078       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
11079                                             (TARGET_GNU_TLS && !TARGET_64BIT)
11080                                             ? "___tls_get_addr"
11081                                             : "__tls_get_addr");
11082     }
11083
11084   return ix86_tls_symbol;
11085 }
11086 \f
11087 /* Calculate the length of the memory address in the instruction
11088    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
11089
11090 int
11091 memory_address_length (rtx addr)
11092 {
11093   struct ix86_address parts;
11094   rtx base, index, disp;
11095   int len;
11096
11097   if (GET_CODE (addr) == PRE_DEC
11098       || GET_CODE (addr) == POST_INC
11099       || GET_CODE (addr) == PRE_MODIFY
11100       || GET_CODE (addr) == POST_MODIFY)
11101     return 0;
11102
11103   if (! ix86_decompose_address (addr, &parts))
11104     abort ();
11105
11106   base = parts.base;
11107   index = parts.index;
11108   disp = parts.disp;
11109   len = 0;
11110
11111   /* Rule of thumb:
11112        - esp as the base always wants an index,
11113        - ebp as the base always wants a displacement.  */
11114
11115   /* Register Indirect.  */
11116   if (base && !index && !disp)
11117     {
11118       /* esp (for its index) and ebp (for its displacement) need
11119          the two-byte modrm form.  */
11120       if (addr == stack_pointer_rtx
11121           || addr == arg_pointer_rtx
11122           || addr == frame_pointer_rtx
11123           || addr == hard_frame_pointer_rtx)
11124         len = 1;
11125     }
11126
11127   /* Direct Addressing.  */
11128   else if (disp && !base && !index)
11129     len = 4;
11130
11131   else
11132     {
11133       /* Find the length of the displacement constant.  */
11134       if (disp)
11135         {
11136           if (GET_CODE (disp) == CONST_INT
11137               && CONST_OK_FOR_LETTER_P (INTVAL (disp), 'K')
11138               && base)
11139             len = 1;
11140           else
11141             len = 4;
11142         }
11143       /* ebp always wants a displacement.  */
11144       else if (base == hard_frame_pointer_rtx)
11145         len = 1;
11146
11147       /* An index requires the two-byte modrm form....  */
11148       if (index
11149           /* ...like esp, which always wants an index.  */
11150           || base == stack_pointer_rtx
11151           || base == arg_pointer_rtx
11152           || base == frame_pointer_rtx)
11153         len += 1;
11154     }
11155
11156   return len;
11157 }
11158
11159 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
11160    is set, expect that insn have 8bit immediate alternative.  */
11161 int
11162 ix86_attr_length_immediate_default (rtx insn, int shortform)
11163 {
11164   int len = 0;
11165   int i;
11166   extract_insn_cached (insn);
11167   for (i = recog_data.n_operands - 1; i >= 0; --i)
11168     if (CONSTANT_P (recog_data.operand[i]))
11169       {
11170         if (len)
11171           abort ();
11172         if (shortform
11173             && GET_CODE (recog_data.operand[i]) == CONST_INT
11174             && CONST_OK_FOR_LETTER_P (INTVAL (recog_data.operand[i]), 'K'))
11175           len = 1;
11176         else
11177           {
11178             switch (get_attr_mode (insn))
11179               {
11180                 case MODE_QI:
11181                   len+=1;
11182                   break;
11183                 case MODE_HI:
11184                   len+=2;
11185                   break;
11186                 case MODE_SI:
11187                   len+=4;
11188                   break;
11189                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
11190                 case MODE_DI:
11191                   len+=4;
11192                   break;
11193                 default:
11194                   fatal_insn ("unknown insn mode", insn);
11195               }
11196           }
11197       }
11198   return len;
11199 }
11200 /* Compute default value for "length_address" attribute.  */
11201 int
11202 ix86_attr_length_address_default (rtx insn)
11203 {
11204   int i;
11205
11206   if (get_attr_type (insn) == TYPE_LEA)
11207     {
11208       rtx set = PATTERN (insn);
11209       if (GET_CODE (set) == SET)
11210         ;
11211       else if (GET_CODE (set) == PARALLEL
11212                && GET_CODE (XVECEXP (set, 0, 0)) == SET)
11213         set = XVECEXP (set, 0, 0);
11214       else
11215         {
11216 #ifdef ENABLE_CHECKING
11217           abort ();
11218 #endif
11219           return 0;
11220         }
11221
11222       return memory_address_length (SET_SRC (set));
11223     }
11224
11225   extract_insn_cached (insn);
11226   for (i = recog_data.n_operands - 1; i >= 0; --i)
11227     if (GET_CODE (recog_data.operand[i]) == MEM)
11228       {
11229         return memory_address_length (XEXP (recog_data.operand[i], 0));
11230         break;
11231       }
11232   return 0;
11233 }
11234 \f
11235 /* Return the maximum number of instructions a cpu can issue.  */
11236
11237 static int
11238 ix86_issue_rate (void)
11239 {
11240   switch (ix86_tune)
11241     {
11242     case PROCESSOR_PENTIUM:
11243     case PROCESSOR_K6:
11244       return 2;
11245
11246     case PROCESSOR_PENTIUMPRO:
11247     case PROCESSOR_PENTIUM4:
11248     case PROCESSOR_ATHLON:
11249     case PROCESSOR_K8:
11250     case PROCESSOR_NOCONA:
11251       return 3;
11252
11253     default:
11254       return 1;
11255     }
11256 }
11257
11258 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
11259    by DEP_INSN and nothing set by DEP_INSN.  */
11260
11261 static int
11262 ix86_flags_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
11263 {
11264   rtx set, set2;
11265
11266   /* Simplify the test for uninteresting insns.  */
11267   if (insn_type != TYPE_SETCC
11268       && insn_type != TYPE_ICMOV
11269       && insn_type != TYPE_FCMOV
11270       && insn_type != TYPE_IBR)
11271     return 0;
11272
11273   if ((set = single_set (dep_insn)) != 0)
11274     {
11275       set = SET_DEST (set);
11276       set2 = NULL_RTX;
11277     }
11278   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
11279            && XVECLEN (PATTERN (dep_insn), 0) == 2
11280            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
11281            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
11282     {
11283       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
11284       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
11285     }
11286   else
11287     return 0;
11288
11289   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
11290     return 0;
11291
11292   /* This test is true if the dependent insn reads the flags but
11293      not any other potentially set register.  */
11294   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
11295     return 0;
11296
11297   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
11298     return 0;
11299
11300   return 1;
11301 }
11302
11303 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
11304    address with operands set by DEP_INSN.  */
11305
11306 static int
11307 ix86_agi_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
11308 {
11309   rtx addr;
11310
11311   if (insn_type == TYPE_LEA
11312       && TARGET_PENTIUM)
11313     {
11314       addr = PATTERN (insn);
11315       if (GET_CODE (addr) == SET)
11316         ;
11317       else if (GET_CODE (addr) == PARALLEL
11318                && GET_CODE (XVECEXP (addr, 0, 0)) == SET)
11319         addr = XVECEXP (addr, 0, 0);
11320       else
11321         abort ();
11322       addr = SET_SRC (addr);
11323     }
11324   else
11325     {
11326       int i;
11327       extract_insn_cached (insn);
11328       for (i = recog_data.n_operands - 1; i >= 0; --i)
11329         if (GET_CODE (recog_data.operand[i]) == MEM)
11330           {
11331             addr = XEXP (recog_data.operand[i], 0);
11332             goto found;
11333           }
11334       return 0;
11335     found:;
11336     }
11337
11338   return modified_in_p (addr, dep_insn);
11339 }
11340
11341 static int
11342 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
11343 {
11344   enum attr_type insn_type, dep_insn_type;
11345   enum attr_memory memory;
11346   rtx set, set2;
11347   int dep_insn_code_number;
11348
11349   /* Anti and output dependencies have zero cost on all CPUs.  */
11350   if (REG_NOTE_KIND (link) != 0)
11351     return 0;
11352
11353   dep_insn_code_number = recog_memoized (dep_insn);
11354
11355   /* If we can't recognize the insns, we can't really do anything.  */
11356   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
11357     return cost;
11358
11359   insn_type = get_attr_type (insn);
11360   dep_insn_type = get_attr_type (dep_insn);
11361
11362   switch (ix86_tune)
11363     {
11364     case PROCESSOR_PENTIUM:
11365       /* Address Generation Interlock adds a cycle of latency.  */
11366       if (ix86_agi_dependant (insn, dep_insn, insn_type))
11367         cost += 1;
11368
11369       /* ??? Compares pair with jump/setcc.  */
11370       if (ix86_flags_dependant (insn, dep_insn, insn_type))
11371         cost = 0;
11372
11373       /* Floating point stores require value to be ready one cycle earlier.  */
11374       if (insn_type == TYPE_FMOV
11375           && get_attr_memory (insn) == MEMORY_STORE
11376           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11377         cost += 1;
11378       break;
11379
11380     case PROCESSOR_PENTIUMPRO:
11381       memory = get_attr_memory (insn);
11382
11383       /* INT->FP conversion is expensive.  */
11384       if (get_attr_fp_int_src (dep_insn))
11385         cost += 5;
11386
11387       /* There is one cycle extra latency between an FP op and a store.  */
11388       if (insn_type == TYPE_FMOV
11389           && (set = single_set (dep_insn)) != NULL_RTX
11390           && (set2 = single_set (insn)) != NULL_RTX
11391           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
11392           && GET_CODE (SET_DEST (set2)) == MEM)
11393         cost += 1;
11394
11395       /* Show ability of reorder buffer to hide latency of load by executing
11396          in parallel with previous instruction in case
11397          previous instruction is not needed to compute the address.  */
11398       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
11399           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11400         {
11401           /* Claim moves to take one cycle, as core can issue one load
11402              at time and the next load can start cycle later.  */
11403           if (dep_insn_type == TYPE_IMOV
11404               || dep_insn_type == TYPE_FMOV)
11405             cost = 1;
11406           else if (cost > 1)
11407             cost--;
11408         }
11409       break;
11410
11411     case PROCESSOR_K6:
11412       memory = get_attr_memory (insn);
11413
11414       /* The esp dependency is resolved before the instruction is really
11415          finished.  */
11416       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
11417           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
11418         return 1;
11419
11420       /* INT->FP conversion is expensive.  */
11421       if (get_attr_fp_int_src (dep_insn))
11422         cost += 5;
11423
11424       /* Show ability of reorder buffer to hide latency of load by executing
11425          in parallel with previous instruction in case
11426          previous instruction is not needed to compute the address.  */
11427       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
11428           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11429         {
11430           /* Claim moves to take one cycle, as core can issue one load
11431              at time and the next load can start cycle later.  */
11432           if (dep_insn_type == TYPE_IMOV
11433               || dep_insn_type == TYPE_FMOV)
11434             cost = 1;
11435           else if (cost > 2)
11436             cost -= 2;
11437           else
11438             cost = 1;
11439         }
11440       break;
11441
11442     case PROCESSOR_ATHLON:
11443     case PROCESSOR_K8:
11444       memory = get_attr_memory (insn);
11445
11446       /* Show ability of reorder buffer to hide latency of load by executing
11447          in parallel with previous instruction in case
11448          previous instruction is not needed to compute the address.  */
11449       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
11450           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11451         {
11452           enum attr_unit unit = get_attr_unit (insn);
11453           int loadcost = 3;
11454
11455           /* Because of the difference between the length of integer and
11456              floating unit pipeline preparation stages, the memory operands
11457              for floating point are cheaper.
11458
11459              ??? For Athlon it the difference is most probably 2.  */
11460           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
11461             loadcost = 3;
11462           else
11463             loadcost = TARGET_ATHLON ? 2 : 0;
11464
11465           if (cost >= loadcost)
11466             cost -= loadcost;
11467           else
11468             cost = 0;
11469         }
11470
11471     default:
11472       break;
11473     }
11474
11475   return cost;
11476 }
11477
11478 /* How many alternative schedules to try.  This should be as wide as the
11479    scheduling freedom in the DFA, but no wider.  Making this value too
11480    large results extra work for the scheduler.  */
11481
11482 static int
11483 ia32_multipass_dfa_lookahead (void)
11484 {
11485   if (ix86_tune == PROCESSOR_PENTIUM)
11486     return 2;
11487
11488   if (ix86_tune == PROCESSOR_PENTIUMPRO
11489       || ix86_tune == PROCESSOR_K6)
11490     return 1;
11491
11492   else
11493     return 0;
11494 }
11495
11496 \f
11497 /* Compute the alignment given to a constant that is being placed in memory.
11498    EXP is the constant and ALIGN is the alignment that the object would
11499    ordinarily have.
11500    The value of this function is used instead of that alignment to align
11501    the object.  */
11502
11503 int
11504 ix86_constant_alignment (tree exp, int align)
11505 {
11506   if (TREE_CODE (exp) == REAL_CST)
11507     {
11508       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
11509         return 64;
11510       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
11511         return 128;
11512     }
11513   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
11514            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
11515     return BITS_PER_WORD;
11516
11517   return align;
11518 }
11519
11520 /* Compute the alignment for a static variable.
11521    TYPE is the data type, and ALIGN is the alignment that
11522    the object would ordinarily have.  The value of this function is used
11523    instead of that alignment to align the object.  */
11524
11525 int
11526 ix86_data_alignment (tree type, int align)
11527 {
11528   if (AGGREGATE_TYPE_P (type)
11529        && TYPE_SIZE (type)
11530        && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
11531        && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 256
11532            || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 256)
11533     return 256;
11534
11535   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
11536      to 16byte boundary.  */
11537   if (TARGET_64BIT)
11538     {
11539       if (AGGREGATE_TYPE_P (type)
11540            && TYPE_SIZE (type)
11541            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
11542            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
11543                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
11544         return 128;
11545     }
11546
11547   if (TREE_CODE (type) == ARRAY_TYPE)
11548     {
11549       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
11550         return 64;
11551       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
11552         return 128;
11553     }
11554   else if (TREE_CODE (type) == COMPLEX_TYPE)
11555     {
11556
11557       if (TYPE_MODE (type) == DCmode && align < 64)
11558         return 64;
11559       if (TYPE_MODE (type) == XCmode && align < 128)
11560         return 128;
11561     }
11562   else if ((TREE_CODE (type) == RECORD_TYPE
11563             || TREE_CODE (type) == UNION_TYPE
11564             || TREE_CODE (type) == QUAL_UNION_TYPE)
11565            && TYPE_FIELDS (type))
11566     {
11567       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
11568         return 64;
11569       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
11570         return 128;
11571     }
11572   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
11573            || TREE_CODE (type) == INTEGER_TYPE)
11574     {
11575       if (TYPE_MODE (type) == DFmode && align < 64)
11576         return 64;
11577       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
11578         return 128;
11579     }
11580
11581   return align;
11582 }
11583
11584 /* Compute the alignment for a local variable.
11585    TYPE is the data type, and ALIGN is the alignment that
11586    the object would ordinarily have.  The value of this macro is used
11587    instead of that alignment to align the object.  */
11588
11589 int
11590 ix86_local_alignment (tree type, int align)
11591 {
11592   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
11593      to 16byte boundary.  */
11594   if (TARGET_64BIT)
11595     {
11596       if (AGGREGATE_TYPE_P (type)
11597            && TYPE_SIZE (type)
11598            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
11599            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
11600                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
11601         return 128;
11602     }
11603   if (TREE_CODE (type) == ARRAY_TYPE)
11604     {
11605       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
11606         return 64;
11607       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
11608         return 128;
11609     }
11610   else if (TREE_CODE (type) == COMPLEX_TYPE)
11611     {
11612       if (TYPE_MODE (type) == DCmode && align < 64)
11613         return 64;
11614       if (TYPE_MODE (type) == XCmode && align < 128)
11615         return 128;
11616     }
11617   else if ((TREE_CODE (type) == RECORD_TYPE
11618             || TREE_CODE (type) == UNION_TYPE
11619             || TREE_CODE (type) == QUAL_UNION_TYPE)
11620            && TYPE_FIELDS (type))
11621     {
11622       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
11623         return 64;
11624       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
11625         return 128;
11626     }
11627   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
11628            || TREE_CODE (type) == INTEGER_TYPE)
11629     {
11630
11631       if (TYPE_MODE (type) == DFmode && align < 64)
11632         return 64;
11633       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
11634         return 128;
11635     }
11636   return align;
11637 }
11638 \f
11639 /* Emit RTL insns to initialize the variable parts of a trampoline.
11640    FNADDR is an RTX for the address of the function's pure code.
11641    CXT is an RTX for the static chain value for the function.  */
11642 void
11643 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
11644 {
11645   if (!TARGET_64BIT)
11646     {
11647       /* Compute offset from the end of the jmp to the target function.  */
11648       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
11649                                plus_constant (tramp, 10),
11650                                NULL_RTX, 1, OPTAB_DIRECT);
11651       emit_move_insn (gen_rtx_MEM (QImode, tramp),
11652                       gen_int_mode (0xb9, QImode));
11653       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
11654       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
11655                       gen_int_mode (0xe9, QImode));
11656       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
11657     }
11658   else
11659     {
11660       int offset = 0;
11661       /* Try to load address using shorter movl instead of movabs.
11662          We may want to support movq for kernel mode, but kernel does not use
11663          trampolines at the moment.  */
11664       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
11665         {
11666           fnaddr = copy_to_mode_reg (DImode, fnaddr);
11667           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
11668                           gen_int_mode (0xbb41, HImode));
11669           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
11670                           gen_lowpart (SImode, fnaddr));
11671           offset += 6;
11672         }
11673       else
11674         {
11675           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
11676                           gen_int_mode (0xbb49, HImode));
11677           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
11678                           fnaddr);
11679           offset += 10;
11680         }
11681       /* Load static chain using movabs to r10.  */
11682       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
11683                       gen_int_mode (0xba49, HImode));
11684       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
11685                       cxt);
11686       offset += 10;
11687       /* Jump to the r11 */
11688       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
11689                       gen_int_mode (0xff49, HImode));
11690       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
11691                       gen_int_mode (0xe3, QImode));
11692       offset += 3;
11693       if (offset > TRAMPOLINE_SIZE)
11694         abort ();
11695     }
11696
11697 #ifdef ENABLE_EXECUTE_STACK
11698   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
11699                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
11700 #endif
11701 }
11702 \f
11703 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
11704 do {                                                                    \
11705   if ((MASK) & target_flags                                             \
11706       && (!((MASK) & MASK_64BIT) || TARGET_64BIT))                      \
11707     lang_hooks.builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,   \
11708                                  NULL, NULL_TREE);                      \
11709 } while (0)
11710
11711 struct builtin_description
11712 {
11713   const unsigned int mask;
11714   const enum insn_code icode;
11715   const char *const name;
11716   const enum ix86_builtins code;
11717   const enum rtx_code comparison;
11718   const unsigned int flag;
11719 };
11720
11721 static const struct builtin_description bdesc_comi[] =
11722 {
11723   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
11724   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
11725   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
11726   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
11727   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
11728   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
11729   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
11730   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
11731   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
11732   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
11733   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
11734   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
11735   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
11736   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
11737   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
11738   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
11739   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
11740   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
11741   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
11742   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
11743   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
11744   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
11745   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
11746   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
11747 };
11748
11749 static const struct builtin_description bdesc_2arg[] =
11750 {
11751   /* SSE */
11752   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
11753   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
11754   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
11755   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
11756   { MASK_SSE, CODE_FOR_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
11757   { MASK_SSE, CODE_FOR_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
11758   { MASK_SSE, CODE_FOR_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
11759   { MASK_SSE, CODE_FOR_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
11760
11761   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
11762   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
11763   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
11764   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, 1 },
11765   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, 1 },
11766   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
11767   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, EQ, 0 },
11768   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, LT, 0 },
11769   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, LE, 0 },
11770   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, LT, 1 },
11771   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, LE, 1 },
11772   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, UNORDERED, 0 },
11773   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
11774   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
11775   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
11776   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
11777   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, EQ, 0 },
11778   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, LT, 0 },
11779   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, LE, 0 },
11780   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
11781
11782   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
11783   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
11784   { MASK_SSE, CODE_FOR_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
11785   { MASK_SSE, CODE_FOR_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
11786
11787   { MASK_SSE, CODE_FOR_sse_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
11788   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
11789   { MASK_SSE, CODE_FOR_sse_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
11790   { MASK_SSE, CODE_FOR_sse_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
11791
11792   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
11793   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
11794   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
11795   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
11796   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
11797
11798   /* MMX */
11799   { MASK_MMX, CODE_FOR_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
11800   { MASK_MMX, CODE_FOR_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
11801   { MASK_MMX, CODE_FOR_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
11802   { MASK_MMX, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
11803   { MASK_MMX, CODE_FOR_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
11804   { MASK_MMX, CODE_FOR_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
11805   { MASK_MMX, CODE_FOR_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
11806   { MASK_MMX, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
11807
11808   { MASK_MMX, CODE_FOR_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
11809   { MASK_MMX, CODE_FOR_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
11810   { MASK_MMX, CODE_FOR_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
11811   { MASK_MMX, CODE_FOR_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
11812   { MASK_MMX, CODE_FOR_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
11813   { MASK_MMX, CODE_FOR_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
11814   { MASK_MMX, CODE_FOR_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
11815   { MASK_MMX, CODE_FOR_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
11816
11817   { MASK_MMX, CODE_FOR_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
11818   { MASK_MMX, CODE_FOR_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
11819   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
11820
11821   { MASK_MMX, CODE_FOR_mmx_anddi3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
11822   { MASK_MMX, CODE_FOR_mmx_nanddi3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
11823   { MASK_MMX, CODE_FOR_mmx_iordi3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
11824   { MASK_MMX, CODE_FOR_mmx_xordi3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
11825
11826   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
11827   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
11828
11829   { MASK_MMX, CODE_FOR_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
11830   { MASK_MMX, CODE_FOR_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
11831   { MASK_MMX, CODE_FOR_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
11832   { MASK_MMX, CODE_FOR_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
11833   { MASK_MMX, CODE_FOR_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
11834   { MASK_MMX, CODE_FOR_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
11835
11836   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
11837   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
11838   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
11839   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
11840
11841   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
11842   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
11843   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
11844   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
11845   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
11846   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
11847
11848   /* Special.  */
11849   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
11850   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
11851   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
11852
11853   { MASK_SSE, CODE_FOR_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
11854   { MASK_SSE, CODE_FOR_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
11855   { MASK_SSE | MASK_64BIT, CODE_FOR_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
11856
11857   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
11858   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
11859   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
11860   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
11861   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
11862   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
11863
11864   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
11865   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
11866   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
11867   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
11868   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
11869   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
11870
11871   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
11872   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
11873   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
11874   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
11875
11876   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
11877   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
11878
11879   /* SSE2 */
11880   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
11881   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
11882   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
11883   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
11884   { MASK_SSE2, CODE_FOR_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
11885   { MASK_SSE2, CODE_FOR_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
11886   { MASK_SSE2, CODE_FOR_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
11887   { MASK_SSE2, CODE_FOR_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
11888
11889   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
11890   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
11891   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
11892   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, 1 },
11893   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, 1 },
11894   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
11895   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, EQ, 0 },
11896   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, LT, 0 },
11897   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, LE, 0 },
11898   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, LT, 1 },
11899   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, LE, 1 },
11900   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, UNORDERED, 0 },
11901   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
11902   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
11903   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
11904   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
11905   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, EQ, 0 },
11906   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, LT, 0 },
11907   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, LE, 0 },
11908   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, UNORDERED, 0 },
11909
11910   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
11911   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
11912   { MASK_SSE2, CODE_FOR_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
11913   { MASK_SSE2, CODE_FOR_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
11914
11915   { MASK_SSE2, CODE_FOR_sse2_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
11916   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
11917   { MASK_SSE2, CODE_FOR_sse2_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
11918   { MASK_SSE2, CODE_FOR_sse2_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
11919
11920   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
11921   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
11922   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
11923
11924   /* SSE2 MMX */
11925   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
11926   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
11927   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
11928   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
11929   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
11930   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
11931   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
11932   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
11933
11934   { MASK_MMX, CODE_FOR_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
11935   { MASK_MMX, CODE_FOR_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
11936   { MASK_MMX, CODE_FOR_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
11937   { MASK_MMX, CODE_FOR_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
11938   { MASK_MMX, CODE_FOR_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
11939   { MASK_MMX, CODE_FOR_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
11940   { MASK_MMX, CODE_FOR_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
11941   { MASK_MMX, CODE_FOR_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
11942
11943   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
11944   { MASK_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
11945
11946   { MASK_SSE2, CODE_FOR_sse2_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
11947   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
11948   { MASK_SSE2, CODE_FOR_sse2_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
11949   { MASK_SSE2, CODE_FOR_sse2_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
11950
11951   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
11952   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
11953
11954   { MASK_SSE2, CODE_FOR_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
11955   { MASK_SSE2, CODE_FOR_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
11956   { MASK_SSE2, CODE_FOR_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
11957   { MASK_SSE2, CODE_FOR_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
11958   { MASK_SSE2, CODE_FOR_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
11959   { MASK_SSE2, CODE_FOR_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
11960
11961   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
11962   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
11963   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
11964   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
11965
11966   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
11967   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
11968   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
11969   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
11970   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
11971   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
11972   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
11973   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
11974
11975   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
11976   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
11977   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
11978
11979   { MASK_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
11980   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
11981
11982   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
11983   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
11984
11985   { MASK_SSE2, CODE_FOR_ashlv8hi3_ti, 0, IX86_BUILTIN_PSLLW128, 0, 0 },
11986   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
11987   { MASK_SSE2, CODE_FOR_ashlv4si3_ti, 0, IX86_BUILTIN_PSLLD128, 0, 0 },
11988   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
11989   { MASK_SSE2, CODE_FOR_ashlv2di3_ti, 0, IX86_BUILTIN_PSLLQ128, 0, 0 },
11990   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
11991
11992   { MASK_SSE2, CODE_FOR_lshrv8hi3_ti, 0, IX86_BUILTIN_PSRLW128, 0, 0 },
11993   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
11994   { MASK_SSE2, CODE_FOR_lshrv4si3_ti, 0, IX86_BUILTIN_PSRLD128, 0, 0 },
11995   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
11996   { MASK_SSE2, CODE_FOR_lshrv2di3_ti, 0, IX86_BUILTIN_PSRLQ128, 0, 0 },
11997   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
11998
11999   { MASK_SSE2, CODE_FOR_ashrv8hi3_ti, 0, IX86_BUILTIN_PSRAW128, 0, 0 },
12000   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
12001   { MASK_SSE2, CODE_FOR_ashrv4si3_ti, 0, IX86_BUILTIN_PSRAD128, 0, 0 },
12002   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
12003
12004   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
12005
12006   { MASK_SSE2, CODE_FOR_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
12007   { MASK_SSE2 | MASK_64BIT, CODE_FOR_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
12008   { MASK_SSE2, CODE_FOR_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
12009   { MASK_SSE2, CODE_FOR_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
12010
12011   /* SSE3 MMX */
12012   { MASK_SSE3, CODE_FOR_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
12013   { MASK_SSE3, CODE_FOR_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
12014   { MASK_SSE3, CODE_FOR_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
12015   { MASK_SSE3, CODE_FOR_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
12016   { MASK_SSE3, CODE_FOR_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
12017   { MASK_SSE3, CODE_FOR_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 }
12018 };
12019
12020 static const struct builtin_description bdesc_1arg[] =
12021 {
12022   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
12023   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
12024
12025   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
12026   { MASK_SSE, CODE_FOR_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
12027   { MASK_SSE, CODE_FOR_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
12028
12029   { MASK_SSE, CODE_FOR_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
12030   { MASK_SSE, CODE_FOR_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
12031   { MASK_SSE | MASK_64BIT, CODE_FOR_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
12032   { MASK_SSE, CODE_FOR_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
12033   { MASK_SSE, CODE_FOR_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
12034   { MASK_SSE | MASK_64BIT, CODE_FOR_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
12035
12036   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
12037   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
12038   { MASK_SSE2, CODE_FOR_sse2_movq2dq, 0, IX86_BUILTIN_MOVQ2DQ, 0, 0 },
12039   { MASK_SSE2, CODE_FOR_sse2_movdq2q, 0, IX86_BUILTIN_MOVDQ2Q, 0, 0 },
12040
12041   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
12042
12043   { MASK_SSE2, CODE_FOR_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
12044   { MASK_SSE2, CODE_FOR_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
12045
12046   { MASK_SSE2, CODE_FOR_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
12047   { MASK_SSE2, CODE_FOR_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
12048   { MASK_SSE2, CODE_FOR_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
12049   { MASK_SSE2, CODE_FOR_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
12050   { MASK_SSE2, CODE_FOR_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
12051
12052   { MASK_SSE2, CODE_FOR_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
12053
12054   { MASK_SSE2, CODE_FOR_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
12055   { MASK_SSE2, CODE_FOR_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
12056   { MASK_SSE2 | MASK_64BIT, CODE_FOR_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
12057   { MASK_SSE2 | MASK_64BIT, CODE_FOR_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
12058
12059   { MASK_SSE2, CODE_FOR_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
12060   { MASK_SSE2, CODE_FOR_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
12061   { MASK_SSE2, CODE_FOR_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
12062
12063   { MASK_SSE2, CODE_FOR_sse2_movq, 0, IX86_BUILTIN_MOVQ, 0, 0 },
12064
12065   /* SSE3 */
12066   { MASK_SSE3, CODE_FOR_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
12067   { MASK_SSE3, CODE_FOR_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
12068   { MASK_SSE3, CODE_FOR_movddup,  0, IX86_BUILTIN_MOVDDUP, 0, 0 }
12069 };
12070
12071 void
12072 ix86_init_builtins (void)
12073 {
12074   if (TARGET_MMX)
12075     ix86_init_mmx_sse_builtins ();
12076 }
12077
12078 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
12079    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
12080    builtins.  */
12081 static void
12082 ix86_init_mmx_sse_builtins (void)
12083 {
12084   const struct builtin_description * d;
12085   size_t i;
12086
12087   tree V16QI_type_node = build_vector_type_for_mode (intQI_type_node, V16QImode);
12088   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12089   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
12090   tree V2DI_type_node = build_vector_type_for_mode (intDI_type_node, V2DImode);
12091   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
12092   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
12093   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
12094   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
12095   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
12096   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
12097
12098   tree pchar_type_node = build_pointer_type (char_type_node);
12099   tree pcchar_type_node = build_pointer_type (
12100                              build_type_variant (char_type_node, 1, 0));
12101   tree pfloat_type_node = build_pointer_type (float_type_node);
12102   tree pcfloat_type_node = build_pointer_type (
12103                              build_type_variant (float_type_node, 1, 0));
12104   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
12105   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
12106   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
12107
12108   /* Comparisons.  */
12109   tree int_ftype_v4sf_v4sf
12110     = build_function_type_list (integer_type_node,
12111                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
12112   tree v4si_ftype_v4sf_v4sf
12113     = build_function_type_list (V4SI_type_node,
12114                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
12115   /* MMX/SSE/integer conversions.  */
12116   tree int_ftype_v4sf
12117     = build_function_type_list (integer_type_node,
12118                                 V4SF_type_node, NULL_TREE);
12119   tree int64_ftype_v4sf
12120     = build_function_type_list (long_long_integer_type_node,
12121                                 V4SF_type_node, NULL_TREE);
12122   tree int_ftype_v8qi
12123     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
12124   tree v4sf_ftype_v4sf_int
12125     = build_function_type_list (V4SF_type_node,
12126                                 V4SF_type_node, integer_type_node, NULL_TREE);
12127   tree v4sf_ftype_v4sf_int64
12128     = build_function_type_list (V4SF_type_node,
12129                                 V4SF_type_node, long_long_integer_type_node,
12130                                 NULL_TREE);
12131   tree v4sf_ftype_v4sf_v2si
12132     = build_function_type_list (V4SF_type_node,
12133                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
12134   tree int_ftype_v4hi_int
12135     = build_function_type_list (integer_type_node,
12136                                 V4HI_type_node, integer_type_node, NULL_TREE);
12137   tree v4hi_ftype_v4hi_int_int
12138     = build_function_type_list (V4HI_type_node, V4HI_type_node,
12139                                 integer_type_node, integer_type_node,
12140                                 NULL_TREE);
12141   /* Miscellaneous.  */
12142   tree v8qi_ftype_v4hi_v4hi
12143     = build_function_type_list (V8QI_type_node,
12144                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
12145   tree v4hi_ftype_v2si_v2si
12146     = build_function_type_list (V4HI_type_node,
12147                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
12148   tree v4sf_ftype_v4sf_v4sf_int
12149     = build_function_type_list (V4SF_type_node,
12150                                 V4SF_type_node, V4SF_type_node,
12151                                 integer_type_node, NULL_TREE);
12152   tree v2si_ftype_v4hi_v4hi
12153     = build_function_type_list (V2SI_type_node,
12154                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
12155   tree v4hi_ftype_v4hi_int
12156     = build_function_type_list (V4HI_type_node,
12157                                 V4HI_type_node, integer_type_node, NULL_TREE);
12158   tree v4hi_ftype_v4hi_di
12159     = build_function_type_list (V4HI_type_node,
12160                                 V4HI_type_node, long_long_unsigned_type_node,
12161                                 NULL_TREE);
12162   tree v2si_ftype_v2si_di
12163     = build_function_type_list (V2SI_type_node,
12164                                 V2SI_type_node, long_long_unsigned_type_node,
12165                                 NULL_TREE);
12166   tree void_ftype_void
12167     = build_function_type (void_type_node, void_list_node);
12168   tree void_ftype_unsigned
12169     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
12170   tree void_ftype_unsigned_unsigned
12171     = build_function_type_list (void_type_node, unsigned_type_node,
12172                                 unsigned_type_node, NULL_TREE);
12173   tree void_ftype_pcvoid_unsigned_unsigned
12174     = build_function_type_list (void_type_node, const_ptr_type_node,
12175                                 unsigned_type_node, unsigned_type_node,
12176                                 NULL_TREE);
12177   tree unsigned_ftype_void
12178     = build_function_type (unsigned_type_node, void_list_node);
12179   tree di_ftype_void
12180     = build_function_type (long_long_unsigned_type_node, void_list_node);
12181   tree v4sf_ftype_void
12182     = build_function_type (V4SF_type_node, void_list_node);
12183   tree v2si_ftype_v4sf
12184     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
12185   /* Loads/stores.  */
12186   tree void_ftype_v8qi_v8qi_pchar
12187     = build_function_type_list (void_type_node,
12188                                 V8QI_type_node, V8QI_type_node,
12189                                 pchar_type_node, NULL_TREE);
12190   tree v4sf_ftype_pcfloat
12191     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
12192   /* @@@ the type is bogus */
12193   tree v4sf_ftype_v4sf_pv2si
12194     = build_function_type_list (V4SF_type_node,
12195                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
12196   tree void_ftype_pv2si_v4sf
12197     = build_function_type_list (void_type_node,
12198                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
12199   tree void_ftype_pfloat_v4sf
12200     = build_function_type_list (void_type_node,
12201                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
12202   tree void_ftype_pdi_di
12203     = build_function_type_list (void_type_node,
12204                                 pdi_type_node, long_long_unsigned_type_node,
12205                                 NULL_TREE);
12206   tree void_ftype_pv2di_v2di
12207     = build_function_type_list (void_type_node,
12208                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
12209   /* Normal vector unops.  */
12210   tree v4sf_ftype_v4sf
12211     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
12212
12213   /* Normal vector binops.  */
12214   tree v4sf_ftype_v4sf_v4sf
12215     = build_function_type_list (V4SF_type_node,
12216                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
12217   tree v8qi_ftype_v8qi_v8qi
12218     = build_function_type_list (V8QI_type_node,
12219                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
12220   tree v4hi_ftype_v4hi_v4hi
12221     = build_function_type_list (V4HI_type_node,
12222                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
12223   tree v2si_ftype_v2si_v2si
12224     = build_function_type_list (V2SI_type_node,
12225                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
12226   tree di_ftype_di_di
12227     = build_function_type_list (long_long_unsigned_type_node,
12228                                 long_long_unsigned_type_node,
12229                                 long_long_unsigned_type_node, NULL_TREE);
12230
12231   tree v2si_ftype_v2sf
12232     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
12233   tree v2sf_ftype_v2si
12234     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
12235   tree v2si_ftype_v2si
12236     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
12237   tree v2sf_ftype_v2sf
12238     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
12239   tree v2sf_ftype_v2sf_v2sf
12240     = build_function_type_list (V2SF_type_node,
12241                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
12242   tree v2si_ftype_v2sf_v2sf
12243     = build_function_type_list (V2SI_type_node,
12244                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
12245   tree pint_type_node    = build_pointer_type (integer_type_node);
12246   tree pcint_type_node = build_pointer_type (
12247                              build_type_variant (integer_type_node, 1, 0));
12248   tree pdouble_type_node = build_pointer_type (double_type_node);
12249   tree pcdouble_type_node = build_pointer_type (
12250                                 build_type_variant (double_type_node, 1, 0));
12251   tree int_ftype_v2df_v2df
12252     = build_function_type_list (integer_type_node,
12253                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
12254
12255   tree ti_ftype_void
12256     = build_function_type (intTI_type_node, void_list_node);
12257   tree v2di_ftype_void
12258     = build_function_type (V2DI_type_node, void_list_node);
12259   tree ti_ftype_ti_ti
12260     = build_function_type_list (intTI_type_node,
12261                                 intTI_type_node, intTI_type_node, NULL_TREE);
12262   tree void_ftype_pcvoid
12263     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
12264   tree v2di_ftype_di
12265     = build_function_type_list (V2DI_type_node,
12266                                 long_long_unsigned_type_node, NULL_TREE);
12267   tree di_ftype_v2di
12268     = build_function_type_list (long_long_unsigned_type_node,
12269                                 V2DI_type_node, NULL_TREE);
12270   tree v4sf_ftype_v4si
12271     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
12272   tree v4si_ftype_v4sf
12273     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
12274   tree v2df_ftype_v4si
12275     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
12276   tree v4si_ftype_v2df
12277     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
12278   tree v2si_ftype_v2df
12279     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
12280   tree v4sf_ftype_v2df
12281     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
12282   tree v2df_ftype_v2si
12283     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
12284   tree v2df_ftype_v4sf
12285     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
12286   tree int_ftype_v2df
12287     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
12288   tree int64_ftype_v2df
12289     = build_function_type_list (long_long_integer_type_node,
12290                                 V2DF_type_node, NULL_TREE);
12291   tree v2df_ftype_v2df_int
12292     = build_function_type_list (V2DF_type_node,
12293                                 V2DF_type_node, integer_type_node, NULL_TREE);
12294   tree v2df_ftype_v2df_int64
12295     = build_function_type_list (V2DF_type_node,
12296                                 V2DF_type_node, long_long_integer_type_node,
12297                                 NULL_TREE);
12298   tree v4sf_ftype_v4sf_v2df
12299     = build_function_type_list (V4SF_type_node,
12300                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
12301   tree v2df_ftype_v2df_v4sf
12302     = build_function_type_list (V2DF_type_node,
12303                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
12304   tree v2df_ftype_v2df_v2df_int
12305     = build_function_type_list (V2DF_type_node,
12306                                 V2DF_type_node, V2DF_type_node,
12307                                 integer_type_node,
12308                                 NULL_TREE);
12309   tree v2df_ftype_v2df_pv2si
12310     = build_function_type_list (V2DF_type_node,
12311                                 V2DF_type_node, pv2si_type_node, NULL_TREE);
12312   tree void_ftype_pv2si_v2df
12313     = build_function_type_list (void_type_node,
12314                                 pv2si_type_node, V2DF_type_node, NULL_TREE);
12315   tree void_ftype_pdouble_v2df
12316     = build_function_type_list (void_type_node,
12317                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
12318   tree void_ftype_pint_int
12319     = build_function_type_list (void_type_node,
12320                                 pint_type_node, integer_type_node, NULL_TREE);
12321   tree void_ftype_v16qi_v16qi_pchar
12322     = build_function_type_list (void_type_node,
12323                                 V16QI_type_node, V16QI_type_node,
12324                                 pchar_type_node, NULL_TREE);
12325   tree v2df_ftype_pcdouble
12326     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
12327   tree v2df_ftype_v2df_v2df
12328     = build_function_type_list (V2DF_type_node,
12329                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
12330   tree v16qi_ftype_v16qi_v16qi
12331     = build_function_type_list (V16QI_type_node,
12332                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
12333   tree v8hi_ftype_v8hi_v8hi
12334     = build_function_type_list (V8HI_type_node,
12335                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
12336   tree v4si_ftype_v4si_v4si
12337     = build_function_type_list (V4SI_type_node,
12338                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
12339   tree v2di_ftype_v2di_v2di
12340     = build_function_type_list (V2DI_type_node,
12341                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
12342   tree v2di_ftype_v2df_v2df
12343     = build_function_type_list (V2DI_type_node,
12344                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
12345   tree v2df_ftype_v2df
12346     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
12347   tree v2df_ftype_double
12348     = build_function_type_list (V2DF_type_node, double_type_node, NULL_TREE);
12349   tree v2df_ftype_double_double
12350     = build_function_type_list (V2DF_type_node,
12351                                 double_type_node, double_type_node, NULL_TREE);
12352   tree int_ftype_v8hi_int
12353     = build_function_type_list (integer_type_node,
12354                                 V8HI_type_node, integer_type_node, NULL_TREE);
12355   tree v8hi_ftype_v8hi_int_int
12356     = build_function_type_list (V8HI_type_node,
12357                                 V8HI_type_node, integer_type_node,
12358                                 integer_type_node, NULL_TREE);
12359   tree v2di_ftype_v2di_int
12360     = build_function_type_list (V2DI_type_node,
12361                                 V2DI_type_node, integer_type_node, NULL_TREE);
12362   tree v4si_ftype_v4si_int
12363     = build_function_type_list (V4SI_type_node,
12364                                 V4SI_type_node, integer_type_node, NULL_TREE);
12365   tree v8hi_ftype_v8hi_int
12366     = build_function_type_list (V8HI_type_node,
12367                                 V8HI_type_node, integer_type_node, NULL_TREE);
12368   tree v8hi_ftype_v8hi_v2di
12369     = build_function_type_list (V8HI_type_node,
12370                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
12371   tree v4si_ftype_v4si_v2di
12372     = build_function_type_list (V4SI_type_node,
12373                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
12374   tree v4si_ftype_v8hi_v8hi
12375     = build_function_type_list (V4SI_type_node,
12376                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
12377   tree di_ftype_v8qi_v8qi
12378     = build_function_type_list (long_long_unsigned_type_node,
12379                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
12380   tree di_ftype_v2si_v2si
12381     = build_function_type_list (long_long_unsigned_type_node,
12382                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
12383   tree v2di_ftype_v16qi_v16qi
12384     = build_function_type_list (V2DI_type_node,
12385                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
12386   tree v2di_ftype_v4si_v4si
12387     = build_function_type_list (V2DI_type_node,
12388                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
12389   tree int_ftype_v16qi
12390     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
12391   tree v16qi_ftype_pcchar
12392     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
12393   tree void_ftype_pchar_v16qi
12394     = build_function_type_list (void_type_node,
12395                                 pchar_type_node, V16QI_type_node, NULL_TREE);
12396   tree v4si_ftype_pcint
12397     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
12398   tree void_ftype_pcint_v4si
12399     = build_function_type_list (void_type_node,
12400                                 pcint_type_node, V4SI_type_node, NULL_TREE);
12401   tree v2di_ftype_v2di
12402     = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
12403
12404   tree float80_type;
12405   tree float128_type;
12406
12407   /* The __float80 type.  */
12408   if (TYPE_MODE (long_double_type_node) == XFmode)
12409     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
12410                                                "__float80");
12411   else
12412     {
12413       /* The __float80 type.  */
12414       float80_type = make_node (REAL_TYPE);
12415       TYPE_PRECISION (float80_type) = 80;
12416       layout_type (float80_type);
12417       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
12418     }
12419
12420   float128_type = make_node (REAL_TYPE);
12421   TYPE_PRECISION (float128_type) = 128;
12422   layout_type (float128_type);
12423   (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
12424
12425   /* Add all builtins that are more or less simple operations on two
12426      operands.  */
12427   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12428     {
12429       /* Use one of the operands; the target can have a different mode for
12430          mask-generating compares.  */
12431       enum machine_mode mode;
12432       tree type;
12433
12434       if (d->name == 0)
12435         continue;
12436       mode = insn_data[d->icode].operand[1].mode;
12437
12438       switch (mode)
12439         {
12440         case V16QImode:
12441           type = v16qi_ftype_v16qi_v16qi;
12442           break;
12443         case V8HImode:
12444           type = v8hi_ftype_v8hi_v8hi;
12445           break;
12446         case V4SImode:
12447           type = v4si_ftype_v4si_v4si;
12448           break;
12449         case V2DImode:
12450           type = v2di_ftype_v2di_v2di;
12451           break;
12452         case V2DFmode:
12453           type = v2df_ftype_v2df_v2df;
12454           break;
12455         case TImode:
12456           type = ti_ftype_ti_ti;
12457           break;
12458         case V4SFmode:
12459           type = v4sf_ftype_v4sf_v4sf;
12460           break;
12461         case V8QImode:
12462           type = v8qi_ftype_v8qi_v8qi;
12463           break;
12464         case V4HImode:
12465           type = v4hi_ftype_v4hi_v4hi;
12466           break;
12467         case V2SImode:
12468           type = v2si_ftype_v2si_v2si;
12469           break;
12470         case DImode:
12471           type = di_ftype_di_di;
12472           break;
12473
12474         default:
12475           abort ();
12476         }
12477
12478       /* Override for comparisons.  */
12479       if (d->icode == CODE_FOR_maskcmpv4sf3
12480           || d->icode == CODE_FOR_maskncmpv4sf3
12481           || d->icode == CODE_FOR_vmmaskcmpv4sf3
12482           || d->icode == CODE_FOR_vmmaskncmpv4sf3)
12483         type = v4si_ftype_v4sf_v4sf;
12484
12485       if (d->icode == CODE_FOR_maskcmpv2df3
12486           || d->icode == CODE_FOR_maskncmpv2df3
12487           || d->icode == CODE_FOR_vmmaskcmpv2df3
12488           || d->icode == CODE_FOR_vmmaskncmpv2df3)
12489         type = v2di_ftype_v2df_v2df;
12490
12491       def_builtin (d->mask, d->name, type, d->code);
12492     }
12493
12494   /* Add the remaining MMX insns with somewhat more complicated types.  */
12495   def_builtin (MASK_MMX, "__builtin_ia32_mmx_zero", di_ftype_void, IX86_BUILTIN_MMX_ZERO);
12496   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
12497   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
12498   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
12499   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
12500
12501   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
12502   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
12503   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
12504
12505   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
12506   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
12507
12508   def_builtin (MASK_MMX, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
12509   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
12510
12511   /* comi/ucomi insns.  */
12512   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
12513     if (d->mask == MASK_SSE2)
12514       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
12515     else
12516       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
12517
12518   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
12519   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
12520   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
12521
12522   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
12523   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
12524   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
12525   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
12526   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
12527   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
12528   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
12529   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
12530   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
12531   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
12532   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
12533
12534   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pextrw", int_ftype_v4hi_int, IX86_BUILTIN_PEXTRW);
12535   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pinsrw", v4hi_ftype_v4hi_int_int, IX86_BUILTIN_PINSRW);
12536
12537   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
12538
12539   def_builtin (MASK_SSE, "__builtin_ia32_loadaps", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADAPS);
12540   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
12541   def_builtin (MASK_SSE, "__builtin_ia32_loadss", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADSS);
12542   def_builtin (MASK_SSE, "__builtin_ia32_storeaps", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREAPS);
12543   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
12544   def_builtin (MASK_SSE, "__builtin_ia32_storess", void_ftype_pfloat_v4sf, IX86_BUILTIN_STORESS);
12545
12546   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
12547   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
12548   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
12549   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
12550
12551   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
12552   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
12553   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
12554   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
12555
12556   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
12557
12558   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
12559
12560   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
12561   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
12562   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
12563   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
12564   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
12565   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
12566
12567   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
12568
12569   /* Original 3DNow!  */
12570   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
12571   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
12572   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
12573   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
12574   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
12575   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
12576   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
12577   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
12578   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
12579   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
12580   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
12581   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
12582   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
12583   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
12584   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
12585   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
12586   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
12587   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
12588   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
12589   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
12590
12591   /* 3DNow! extension as used in the Athlon CPU.  */
12592   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
12593   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
12594   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
12595   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
12596   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
12597   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
12598
12599   def_builtin (MASK_SSE, "__builtin_ia32_setzerops", v4sf_ftype_void, IX86_BUILTIN_SSE_ZERO);
12600
12601   /* SSE2 */
12602   def_builtin (MASK_SSE2, "__builtin_ia32_pextrw128", int_ftype_v8hi_int, IX86_BUILTIN_PEXTRW128);
12603   def_builtin (MASK_SSE2, "__builtin_ia32_pinsrw128", v8hi_ftype_v8hi_int_int, IX86_BUILTIN_PINSRW128);
12604
12605   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
12606   def_builtin (MASK_SSE2, "__builtin_ia32_movq2dq", v2di_ftype_di, IX86_BUILTIN_MOVQ2DQ);
12607   def_builtin (MASK_SSE2, "__builtin_ia32_movdq2q", di_ftype_v2di, IX86_BUILTIN_MOVDQ2Q);
12608
12609   def_builtin (MASK_SSE2, "__builtin_ia32_loadapd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADAPD);
12610   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
12611   def_builtin (MASK_SSE2, "__builtin_ia32_loadsd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADSD);
12612   def_builtin (MASK_SSE2, "__builtin_ia32_storeapd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREAPD);
12613   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
12614   def_builtin (MASK_SSE2, "__builtin_ia32_storesd", void_ftype_pdouble_v2df, IX86_BUILTIN_STORESD);
12615
12616   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pv2si, IX86_BUILTIN_LOADHPD);
12617   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pv2si, IX86_BUILTIN_LOADLPD);
12618   def_builtin (MASK_SSE2, "__builtin_ia32_storehpd", void_ftype_pv2si_v2df, IX86_BUILTIN_STOREHPD);
12619   def_builtin (MASK_SSE2, "__builtin_ia32_storelpd", void_ftype_pv2si_v2df, IX86_BUILTIN_STORELPD);
12620
12621   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
12622   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
12623   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
12624   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
12625   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
12626
12627   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
12628   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
12629   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
12630   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
12631
12632   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
12633   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
12634
12635   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
12636
12637   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
12638   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
12639
12640   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
12641   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
12642   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
12643   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
12644   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
12645
12646   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
12647
12648   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
12649   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
12650   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
12651   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
12652
12653   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
12654   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
12655   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
12656
12657   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
12658   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
12659   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
12660   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
12661
12662   def_builtin (MASK_SSE2, "__builtin_ia32_setpd1", v2df_ftype_double, IX86_BUILTIN_SETPD1);
12663   def_builtin (MASK_SSE2, "__builtin_ia32_setpd", v2df_ftype_double_double, IX86_BUILTIN_SETPD);
12664   def_builtin (MASK_SSE2, "__builtin_ia32_setzeropd", ti_ftype_void, IX86_BUILTIN_CLRPD);
12665   def_builtin (MASK_SSE2, "__builtin_ia32_loadpd1", v2df_ftype_pcdouble, IX86_BUILTIN_LOADPD1);
12666   def_builtin (MASK_SSE2, "__builtin_ia32_loadrpd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADRPD);
12667   def_builtin (MASK_SSE2, "__builtin_ia32_storepd1", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREPD1);
12668   def_builtin (MASK_SSE2, "__builtin_ia32_storerpd", void_ftype_pdouble_v2df, IX86_BUILTIN_STORERPD);
12669
12670   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
12671   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
12672   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
12673
12674   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqa", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQA);
12675   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
12676   def_builtin (MASK_SSE2, "__builtin_ia32_loadd", v4si_ftype_pcint, IX86_BUILTIN_LOADD);
12677   def_builtin (MASK_SSE2, "__builtin_ia32_storedqa", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQA);
12678   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
12679   def_builtin (MASK_SSE2, "__builtin_ia32_stored", void_ftype_pcint_v4si, IX86_BUILTIN_STORED);
12680   def_builtin (MASK_SSE2, "__builtin_ia32_movq", v2di_ftype_v2di, IX86_BUILTIN_MOVQ);
12681
12682   def_builtin (MASK_SSE, "__builtin_ia32_setzero128", v2di_ftype_void, IX86_BUILTIN_CLRTI);
12683
12684   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
12685   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
12686
12687   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
12688   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
12689   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
12690
12691   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
12692   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
12693   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
12694
12695   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
12696   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
12697
12698   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
12699   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
12700   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
12701   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
12702
12703   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
12704   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
12705   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
12706   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
12707
12708   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
12709   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
12710
12711   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
12712
12713   /* Prescott New Instructions.  */
12714   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
12715                void_ftype_pcvoid_unsigned_unsigned,
12716                IX86_BUILTIN_MONITOR);
12717   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
12718                void_ftype_unsigned_unsigned,
12719                IX86_BUILTIN_MWAIT);
12720   def_builtin (MASK_SSE3, "__builtin_ia32_movshdup",
12721                v4sf_ftype_v4sf,
12722                IX86_BUILTIN_MOVSHDUP);
12723   def_builtin (MASK_SSE3, "__builtin_ia32_movsldup",
12724                v4sf_ftype_v4sf,
12725                IX86_BUILTIN_MOVSLDUP);
12726   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
12727                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
12728   def_builtin (MASK_SSE3, "__builtin_ia32_loadddup",
12729                v2df_ftype_pcdouble, IX86_BUILTIN_LOADDDUP);
12730   def_builtin (MASK_SSE3, "__builtin_ia32_movddup",
12731                v2df_ftype_v2df, IX86_BUILTIN_MOVDDUP);
12732 }
12733
12734 /* Errors in the source file can cause expand_expr to return const0_rtx
12735    where we expect a vector.  To avoid crashing, use one of the vector
12736    clear instructions.  */
12737 static rtx
12738 safe_vector_operand (rtx x, enum machine_mode mode)
12739 {
12740   if (x != const0_rtx)
12741     return x;
12742   x = gen_reg_rtx (mode);
12743
12744   if (VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode))
12745     emit_insn (gen_mmx_clrdi (mode == DImode ? x
12746                               : gen_rtx_SUBREG (DImode, x, 0)));
12747   else
12748     emit_insn (gen_sse_clrv4sf (mode == V4SFmode ? x
12749                                 : gen_rtx_SUBREG (V4SFmode, x, 0),
12750                                 CONST0_RTX (V4SFmode)));
12751   return x;
12752 }
12753
12754 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
12755
12756 static rtx
12757 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
12758 {
12759   rtx pat;
12760   tree arg0 = TREE_VALUE (arglist);
12761   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12762   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12763   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12764   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12765   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12766   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12767
12768   if (VECTOR_MODE_P (mode0))
12769     op0 = safe_vector_operand (op0, mode0);
12770   if (VECTOR_MODE_P (mode1))
12771     op1 = safe_vector_operand (op1, mode1);
12772
12773   if (! target
12774       || GET_MODE (target) != tmode
12775       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12776     target = gen_reg_rtx (tmode);
12777
12778   if (GET_MODE (op1) == SImode && mode1 == TImode)
12779     {
12780       rtx x = gen_reg_rtx (V4SImode);
12781       emit_insn (gen_sse2_loadd (x, op1));
12782       op1 = gen_lowpart (TImode, x);
12783     }
12784
12785   /* In case the insn wants input operands in modes different from
12786      the result, abort.  */
12787   if ((GET_MODE (op0) != mode0 && GET_MODE (op0) != VOIDmode)
12788       || (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode))
12789     abort ();
12790
12791   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12792     op0 = copy_to_mode_reg (mode0, op0);
12793   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12794     op1 = copy_to_mode_reg (mode1, op1);
12795
12796   /* In the commutative cases, both op0 and op1 are nonimmediate_operand,
12797      yet one of the two must not be a memory.  This is normally enforced
12798      by expanders, but we didn't bother to create one here.  */
12799   if (GET_CODE (op0) == MEM && GET_CODE (op1) == MEM)
12800     op0 = copy_to_mode_reg (mode0, op0);
12801
12802   pat = GEN_FCN (icode) (target, op0, op1);
12803   if (! pat)
12804     return 0;
12805   emit_insn (pat);
12806   return target;
12807 }
12808
12809 /* Subroutine of ix86_expand_builtin to take care of stores.  */
12810
12811 static rtx
12812 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
12813 {
12814   rtx pat;
12815   tree arg0 = TREE_VALUE (arglist);
12816   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12817   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12818   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12819   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
12820   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
12821
12822   if (VECTOR_MODE_P (mode1))
12823     op1 = safe_vector_operand (op1, mode1);
12824
12825   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12826   op1 = copy_to_mode_reg (mode1, op1);
12827
12828   pat = GEN_FCN (icode) (op0, op1);
12829   if (pat)
12830     emit_insn (pat);
12831   return 0;
12832 }
12833
12834 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
12835
12836 static rtx
12837 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
12838                           rtx target, int do_load)
12839 {
12840   rtx pat;
12841   tree arg0 = TREE_VALUE (arglist);
12842   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12843   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12844   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12845
12846   if (! target
12847       || GET_MODE (target) != tmode
12848       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12849     target = gen_reg_rtx (tmode);
12850   if (do_load)
12851     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12852   else
12853     {
12854       if (VECTOR_MODE_P (mode0))
12855         op0 = safe_vector_operand (op0, mode0);
12856
12857       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12858         op0 = copy_to_mode_reg (mode0, op0);
12859     }
12860
12861   pat = GEN_FCN (icode) (target, op0);
12862   if (! pat)
12863     return 0;
12864   emit_insn (pat);
12865   return target;
12866 }
12867
12868 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
12869    sqrtss, rsqrtss, rcpss.  */
12870
12871 static rtx
12872 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
12873 {
12874   rtx pat;
12875   tree arg0 = TREE_VALUE (arglist);
12876   rtx op1, op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12877   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12878   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12879
12880   if (! target
12881       || GET_MODE (target) != tmode
12882       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12883     target = gen_reg_rtx (tmode);
12884
12885   if (VECTOR_MODE_P (mode0))
12886     op0 = safe_vector_operand (op0, mode0);
12887
12888   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12889     op0 = copy_to_mode_reg (mode0, op0);
12890
12891   op1 = op0;
12892   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
12893     op1 = copy_to_mode_reg (mode0, op1);
12894
12895   pat = GEN_FCN (icode) (target, op0, op1);
12896   if (! pat)
12897     return 0;
12898   emit_insn (pat);
12899   return target;
12900 }
12901
12902 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
12903
12904 static rtx
12905 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
12906                          rtx target)
12907 {
12908   rtx pat;
12909   tree arg0 = TREE_VALUE (arglist);
12910   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12911   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12912   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12913   rtx op2;
12914   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
12915   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
12916   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
12917   enum rtx_code comparison = d->comparison;
12918
12919   if (VECTOR_MODE_P (mode0))
12920     op0 = safe_vector_operand (op0, mode0);
12921   if (VECTOR_MODE_P (mode1))
12922     op1 = safe_vector_operand (op1, mode1);
12923
12924   /* Swap operands if we have a comparison that isn't available in
12925      hardware.  */
12926   if (d->flag)
12927     {
12928       rtx tmp = gen_reg_rtx (mode1);
12929       emit_move_insn (tmp, op1);
12930       op1 = op0;
12931       op0 = tmp;
12932     }
12933
12934   if (! target
12935       || GET_MODE (target) != tmode
12936       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
12937     target = gen_reg_rtx (tmode);
12938
12939   if (! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
12940     op0 = copy_to_mode_reg (mode0, op0);
12941   if (! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
12942     op1 = copy_to_mode_reg (mode1, op1);
12943
12944   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
12945   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
12946   if (! pat)
12947     return 0;
12948   emit_insn (pat);
12949   return target;
12950 }
12951
12952 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
12953
12954 static rtx
12955 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
12956                       rtx target)
12957 {
12958   rtx pat;
12959   tree arg0 = TREE_VALUE (arglist);
12960   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12961   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12962   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12963   rtx op2;
12964   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
12965   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
12966   enum rtx_code comparison = d->comparison;
12967
12968   if (VECTOR_MODE_P (mode0))
12969     op0 = safe_vector_operand (op0, mode0);
12970   if (VECTOR_MODE_P (mode1))
12971     op1 = safe_vector_operand (op1, mode1);
12972
12973   /* Swap operands if we have a comparison that isn't available in
12974      hardware.  */
12975   if (d->flag)
12976     {
12977       rtx tmp = op1;
12978       op1 = op0;
12979       op0 = tmp;
12980     }
12981
12982   target = gen_reg_rtx (SImode);
12983   emit_move_insn (target, const0_rtx);
12984   target = gen_rtx_SUBREG (QImode, target, 0);
12985
12986   if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
12987     op0 = copy_to_mode_reg (mode0, op0);
12988   if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
12989     op1 = copy_to_mode_reg (mode1, op1);
12990
12991   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
12992   pat = GEN_FCN (d->icode) (op0, op1);
12993   if (! pat)
12994     return 0;
12995   emit_insn (pat);
12996   emit_insn (gen_rtx_SET (VOIDmode,
12997                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
12998                           gen_rtx_fmt_ee (comparison, QImode,
12999                                           SET_DEST (pat),
13000                                           const0_rtx)));
13001
13002   return SUBREG_REG (target);
13003 }
13004
13005 /* Expand an expression EXP that calls a built-in function,
13006    with result going to TARGET if that's convenient
13007    (and in mode MODE if that's convenient).
13008    SUBTARGET may be used as the target for computing one of EXP's operands.
13009    IGNORE is nonzero if the value is to be ignored.  */
13010
13011 rtx
13012 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13013                      enum machine_mode mode ATTRIBUTE_UNUSED,
13014                      int ignore ATTRIBUTE_UNUSED)
13015 {
13016   const struct builtin_description *d;
13017   size_t i;
13018   enum insn_code icode;
13019   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
13020   tree arglist = TREE_OPERAND (exp, 1);
13021   tree arg0, arg1, arg2;
13022   rtx op0, op1, op2, pat;
13023   enum machine_mode tmode, mode0, mode1, mode2;
13024   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
13025
13026   switch (fcode)
13027     {
13028     case IX86_BUILTIN_EMMS:
13029       emit_insn (gen_emms ());
13030       return 0;
13031
13032     case IX86_BUILTIN_SFENCE:
13033       emit_insn (gen_sfence ());
13034       return 0;
13035
13036     case IX86_BUILTIN_PEXTRW:
13037     case IX86_BUILTIN_PEXTRW128:
13038       icode = (fcode == IX86_BUILTIN_PEXTRW
13039                ? CODE_FOR_mmx_pextrw
13040                : CODE_FOR_sse2_pextrw);
13041       arg0 = TREE_VALUE (arglist);
13042       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13043       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13044       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13045       tmode = insn_data[icode].operand[0].mode;
13046       mode0 = insn_data[icode].operand[1].mode;
13047       mode1 = insn_data[icode].operand[2].mode;
13048
13049       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13050         op0 = copy_to_mode_reg (mode0, op0);
13051       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13052         {
13053           error ("selector must be an integer constant in the range 0..%i",
13054                   fcode == IX86_BUILTIN_PEXTRW ? 3:7);
13055           return gen_reg_rtx (tmode);
13056         }
13057       if (target == 0
13058           || GET_MODE (target) != tmode
13059           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13060         target = gen_reg_rtx (tmode);
13061       pat = GEN_FCN (icode) (target, op0, op1);
13062       if (! pat)
13063         return 0;
13064       emit_insn (pat);
13065       return target;
13066
13067     case IX86_BUILTIN_PINSRW:
13068     case IX86_BUILTIN_PINSRW128:
13069       icode = (fcode == IX86_BUILTIN_PINSRW
13070                ? CODE_FOR_mmx_pinsrw
13071                : CODE_FOR_sse2_pinsrw);
13072       arg0 = TREE_VALUE (arglist);
13073       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13074       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
13075       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13076       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13077       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
13078       tmode = insn_data[icode].operand[0].mode;
13079       mode0 = insn_data[icode].operand[1].mode;
13080       mode1 = insn_data[icode].operand[2].mode;
13081       mode2 = insn_data[icode].operand[3].mode;
13082
13083       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13084         op0 = copy_to_mode_reg (mode0, op0);
13085       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13086         op1 = copy_to_mode_reg (mode1, op1);
13087       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13088         {
13089           error ("selector must be an integer constant in the range 0..%i",
13090                   fcode == IX86_BUILTIN_PINSRW ? 15:255);
13091           return const0_rtx;
13092         }
13093       if (target == 0
13094           || GET_MODE (target) != tmode
13095           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13096         target = gen_reg_rtx (tmode);
13097       pat = GEN_FCN (icode) (target, op0, op1, op2);
13098       if (! pat)
13099         return 0;
13100       emit_insn (pat);
13101       return target;
13102
13103     case IX86_BUILTIN_MASKMOVQ:
13104     case IX86_BUILTIN_MASKMOVDQU:
13105       icode = (fcode == IX86_BUILTIN_MASKMOVQ
13106                ? (TARGET_64BIT ? CODE_FOR_mmx_maskmovq_rex : CODE_FOR_mmx_maskmovq)
13107                : (TARGET_64BIT ? CODE_FOR_sse2_maskmovdqu_rex64
13108                   : CODE_FOR_sse2_maskmovdqu));
13109       /* Note the arg order is different from the operand order.  */
13110       arg1 = TREE_VALUE (arglist);
13111       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
13112       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
13113       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13114       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13115       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
13116       mode0 = insn_data[icode].operand[0].mode;
13117       mode1 = insn_data[icode].operand[1].mode;
13118       mode2 = insn_data[icode].operand[2].mode;
13119
13120       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13121         op0 = copy_to_mode_reg (mode0, op0);
13122       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13123         op1 = copy_to_mode_reg (mode1, op1);
13124       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
13125         op2 = copy_to_mode_reg (mode2, op2);
13126       pat = GEN_FCN (icode) (op0, op1, op2);
13127       if (! pat)
13128         return 0;
13129       emit_insn (pat);
13130       return 0;
13131
13132     case IX86_BUILTIN_SQRTSS:
13133       return ix86_expand_unop1_builtin (CODE_FOR_vmsqrtv4sf2, arglist, target);
13134     case IX86_BUILTIN_RSQRTSS:
13135       return ix86_expand_unop1_builtin (CODE_FOR_vmrsqrtv4sf2, arglist, target);
13136     case IX86_BUILTIN_RCPSS:
13137       return ix86_expand_unop1_builtin (CODE_FOR_vmrcpv4sf2, arglist, target);
13138
13139     case IX86_BUILTIN_LOADAPS:
13140       return ix86_expand_unop_builtin (CODE_FOR_sse_movaps, arglist, target, 1);
13141
13142     case IX86_BUILTIN_LOADUPS:
13143       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
13144
13145     case IX86_BUILTIN_STOREAPS:
13146       return ix86_expand_store_builtin (CODE_FOR_sse_movaps, arglist);
13147
13148     case IX86_BUILTIN_STOREUPS:
13149       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
13150
13151     case IX86_BUILTIN_LOADSS:
13152       return ix86_expand_unop_builtin (CODE_FOR_sse_loadss, arglist, target, 1);
13153
13154     case IX86_BUILTIN_STORESS:
13155       return ix86_expand_store_builtin (CODE_FOR_sse_storess, arglist);
13156
13157     case IX86_BUILTIN_LOADHPS:
13158     case IX86_BUILTIN_LOADLPS:
13159     case IX86_BUILTIN_LOADHPD:
13160     case IX86_BUILTIN_LOADLPD:
13161       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_movhps
13162                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_movlps
13163                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_movhpd
13164                : CODE_FOR_sse2_movsd);
13165       arg0 = TREE_VALUE (arglist);
13166       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13167       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13168       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13169       tmode = insn_data[icode].operand[0].mode;
13170       mode0 = insn_data[icode].operand[1].mode;
13171       mode1 = insn_data[icode].operand[2].mode;
13172
13173       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13174         op0 = copy_to_mode_reg (mode0, op0);
13175       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
13176       if (target == 0
13177           || GET_MODE (target) != tmode
13178           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13179         target = gen_reg_rtx (tmode);
13180       pat = GEN_FCN (icode) (target, op0, op1);
13181       if (! pat)
13182         return 0;
13183       emit_insn (pat);
13184       return target;
13185
13186     case IX86_BUILTIN_STOREHPS:
13187     case IX86_BUILTIN_STORELPS:
13188     case IX86_BUILTIN_STOREHPD:
13189     case IX86_BUILTIN_STORELPD:
13190       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_movhps
13191                : fcode == IX86_BUILTIN_STORELPS ? CODE_FOR_sse_movlps
13192                : fcode == IX86_BUILTIN_STOREHPD ? CODE_FOR_sse2_movhpd
13193                : CODE_FOR_sse2_movsd);
13194       arg0 = TREE_VALUE (arglist);
13195       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13196       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13197       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13198       mode0 = insn_data[icode].operand[1].mode;
13199       mode1 = insn_data[icode].operand[2].mode;
13200
13201       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13202       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13203         op1 = copy_to_mode_reg (mode1, op1);
13204
13205       pat = GEN_FCN (icode) (op0, op0, op1);
13206       if (! pat)
13207         return 0;
13208       emit_insn (pat);
13209       return 0;
13210
13211     case IX86_BUILTIN_MOVNTPS:
13212       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
13213     case IX86_BUILTIN_MOVNTQ:
13214       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
13215
13216     case IX86_BUILTIN_LDMXCSR:
13217       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
13218       target = assign_386_stack_local (SImode, 0);
13219       emit_move_insn (target, op0);
13220       emit_insn (gen_ldmxcsr (target));
13221       return 0;
13222
13223     case IX86_BUILTIN_STMXCSR:
13224       target = assign_386_stack_local (SImode, 0);
13225       emit_insn (gen_stmxcsr (target));
13226       return copy_to_mode_reg (SImode, target);
13227
13228     case IX86_BUILTIN_SHUFPS:
13229     case IX86_BUILTIN_SHUFPD:
13230       icode = (fcode == IX86_BUILTIN_SHUFPS
13231                ? CODE_FOR_sse_shufps
13232                : CODE_FOR_sse2_shufpd);
13233       arg0 = TREE_VALUE (arglist);
13234       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13235       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
13236       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13237       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13238       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
13239       tmode = insn_data[icode].operand[0].mode;
13240       mode0 = insn_data[icode].operand[1].mode;
13241       mode1 = insn_data[icode].operand[2].mode;
13242       mode2 = insn_data[icode].operand[3].mode;
13243
13244       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13245         op0 = copy_to_mode_reg (mode0, op0);
13246       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13247         op1 = copy_to_mode_reg (mode1, op1);
13248       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13249         {
13250           /* @@@ better error message */
13251           error ("mask must be an immediate");
13252           return gen_reg_rtx (tmode);
13253         }
13254       if (target == 0
13255           || GET_MODE (target) != tmode
13256           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13257         target = gen_reg_rtx (tmode);
13258       pat = GEN_FCN (icode) (target, op0, op1, op2);
13259       if (! pat)
13260         return 0;
13261       emit_insn (pat);
13262       return target;
13263
13264     case IX86_BUILTIN_PSHUFW:
13265     case IX86_BUILTIN_PSHUFD:
13266     case IX86_BUILTIN_PSHUFHW:
13267     case IX86_BUILTIN_PSHUFLW:
13268       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
13269                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
13270                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
13271                : CODE_FOR_mmx_pshufw);
13272       arg0 = TREE_VALUE (arglist);
13273       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13274       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13275       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13276       tmode = insn_data[icode].operand[0].mode;
13277       mode1 = insn_data[icode].operand[1].mode;
13278       mode2 = insn_data[icode].operand[2].mode;
13279
13280       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
13281         op0 = copy_to_mode_reg (mode1, op0);
13282       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
13283         {
13284           /* @@@ better error message */
13285           error ("mask must be an immediate");
13286           return const0_rtx;
13287         }
13288       if (target == 0
13289           || GET_MODE (target) != tmode
13290           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13291         target = gen_reg_rtx (tmode);
13292       pat = GEN_FCN (icode) (target, op0, op1);
13293       if (! pat)
13294         return 0;
13295       emit_insn (pat);
13296       return target;
13297
13298     case IX86_BUILTIN_PSLLDQI128:
13299     case IX86_BUILTIN_PSRLDQI128:
13300       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
13301                : CODE_FOR_sse2_lshrti3);
13302       arg0 = TREE_VALUE (arglist);
13303       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13304       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13305       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13306       tmode = insn_data[icode].operand[0].mode;
13307       mode1 = insn_data[icode].operand[1].mode;
13308       mode2 = insn_data[icode].operand[2].mode;
13309
13310       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
13311         {
13312           op0 = copy_to_reg (op0);
13313           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
13314         }
13315       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
13316         {
13317           error ("shift must be an immediate");
13318           return const0_rtx;
13319         }
13320       target = gen_reg_rtx (V2DImode);
13321       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
13322       if (! pat)
13323         return 0;
13324       emit_insn (pat);
13325       return target;
13326
13327     case IX86_BUILTIN_FEMMS:
13328       emit_insn (gen_femms ());
13329       return NULL_RTX;
13330
13331     case IX86_BUILTIN_PAVGUSB:
13332       return ix86_expand_binop_builtin (CODE_FOR_pavgusb, arglist, target);
13333
13334     case IX86_BUILTIN_PF2ID:
13335       return ix86_expand_unop_builtin (CODE_FOR_pf2id, arglist, target, 0);
13336
13337     case IX86_BUILTIN_PFACC:
13338       return ix86_expand_binop_builtin (CODE_FOR_pfacc, arglist, target);
13339
13340     case IX86_BUILTIN_PFADD:
13341      return ix86_expand_binop_builtin (CODE_FOR_addv2sf3, arglist, target);
13342
13343     case IX86_BUILTIN_PFCMPEQ:
13344       return ix86_expand_binop_builtin (CODE_FOR_eqv2sf3, arglist, target);
13345
13346     case IX86_BUILTIN_PFCMPGE:
13347       return ix86_expand_binop_builtin (CODE_FOR_gev2sf3, arglist, target);
13348
13349     case IX86_BUILTIN_PFCMPGT:
13350       return ix86_expand_binop_builtin (CODE_FOR_gtv2sf3, arglist, target);
13351
13352     case IX86_BUILTIN_PFMAX:
13353       return ix86_expand_binop_builtin (CODE_FOR_pfmaxv2sf3, arglist, target);
13354
13355     case IX86_BUILTIN_PFMIN:
13356       return ix86_expand_binop_builtin (CODE_FOR_pfminv2sf3, arglist, target);
13357
13358     case IX86_BUILTIN_PFMUL:
13359       return ix86_expand_binop_builtin (CODE_FOR_mulv2sf3, arglist, target);
13360
13361     case IX86_BUILTIN_PFRCP:
13362       return ix86_expand_unop_builtin (CODE_FOR_pfrcpv2sf2, arglist, target, 0);
13363
13364     case IX86_BUILTIN_PFRCPIT1:
13365       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit1v2sf3, arglist, target);
13366
13367     case IX86_BUILTIN_PFRCPIT2:
13368       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit2v2sf3, arglist, target);
13369
13370     case IX86_BUILTIN_PFRSQIT1:
13371       return ix86_expand_binop_builtin (CODE_FOR_pfrsqit1v2sf3, arglist, target);
13372
13373     case IX86_BUILTIN_PFRSQRT:
13374       return ix86_expand_unop_builtin (CODE_FOR_pfrsqrtv2sf2, arglist, target, 0);
13375
13376     case IX86_BUILTIN_PFSUB:
13377       return ix86_expand_binop_builtin (CODE_FOR_subv2sf3, arglist, target);
13378
13379     case IX86_BUILTIN_PFSUBR:
13380       return ix86_expand_binop_builtin (CODE_FOR_subrv2sf3, arglist, target);
13381
13382     case IX86_BUILTIN_PI2FD:
13383       return ix86_expand_unop_builtin (CODE_FOR_floatv2si2, arglist, target, 0);
13384
13385     case IX86_BUILTIN_PMULHRW:
13386       return ix86_expand_binop_builtin (CODE_FOR_pmulhrwv4hi3, arglist, target);
13387
13388     case IX86_BUILTIN_PF2IW:
13389       return ix86_expand_unop_builtin (CODE_FOR_pf2iw, arglist, target, 0);
13390
13391     case IX86_BUILTIN_PFNACC:
13392       return ix86_expand_binop_builtin (CODE_FOR_pfnacc, arglist, target);
13393
13394     case IX86_BUILTIN_PFPNACC:
13395       return ix86_expand_binop_builtin (CODE_FOR_pfpnacc, arglist, target);
13396
13397     case IX86_BUILTIN_PI2FW:
13398       return ix86_expand_unop_builtin (CODE_FOR_pi2fw, arglist, target, 0);
13399
13400     case IX86_BUILTIN_PSWAPDSI:
13401       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2si2, arglist, target, 0);
13402
13403     case IX86_BUILTIN_PSWAPDSF:
13404       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2sf2, arglist, target, 0);
13405
13406     case IX86_BUILTIN_SSE_ZERO:
13407       target = gen_reg_rtx (V4SFmode);
13408       emit_insn (gen_sse_clrv4sf (target, CONST0_RTX (V4SFmode)));
13409       return target;
13410
13411     case IX86_BUILTIN_MMX_ZERO:
13412       target = gen_reg_rtx (DImode);
13413       emit_insn (gen_mmx_clrdi (target));
13414       return target;
13415
13416     case IX86_BUILTIN_CLRTI:
13417       target = gen_reg_rtx (V2DImode);
13418       emit_insn (gen_sse2_clrti (simplify_gen_subreg (TImode, target, V2DImode, 0)));
13419       return target;
13420
13421
13422     case IX86_BUILTIN_SQRTSD:
13423       return ix86_expand_unop1_builtin (CODE_FOR_vmsqrtv2df2, arglist, target);
13424     case IX86_BUILTIN_LOADAPD:
13425       return ix86_expand_unop_builtin (CODE_FOR_sse2_movapd, arglist, target, 1);
13426     case IX86_BUILTIN_LOADUPD:
13427       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
13428
13429     case IX86_BUILTIN_STOREAPD:
13430       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
13431     case IX86_BUILTIN_STOREUPD:
13432       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
13433
13434     case IX86_BUILTIN_LOADSD:
13435       return ix86_expand_unop_builtin (CODE_FOR_sse2_loadsd, arglist, target, 1);
13436
13437     case IX86_BUILTIN_STORESD:
13438       return ix86_expand_store_builtin (CODE_FOR_sse2_storesd, arglist);
13439
13440     case IX86_BUILTIN_SETPD1:
13441       target = assign_386_stack_local (DFmode, 0);
13442       arg0 = TREE_VALUE (arglist);
13443       emit_move_insn (adjust_address (target, DFmode, 0),
13444                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
13445       op0 = gen_reg_rtx (V2DFmode);
13446       emit_insn (gen_sse2_loadsd (op0, adjust_address (target, V2DFmode, 0)));
13447       emit_insn (gen_sse2_shufpd (op0, op0, op0, const0_rtx));
13448       return op0;
13449
13450     case IX86_BUILTIN_SETPD:
13451       target = assign_386_stack_local (V2DFmode, 0);
13452       arg0 = TREE_VALUE (arglist);
13453       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13454       emit_move_insn (adjust_address (target, DFmode, 0),
13455                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
13456       emit_move_insn (adjust_address (target, DFmode, 8),
13457                       expand_expr (arg1, NULL_RTX, VOIDmode, 0));
13458       op0 = gen_reg_rtx (V2DFmode);
13459       emit_insn (gen_sse2_movapd (op0, target));
13460       return op0;
13461
13462     case IX86_BUILTIN_LOADRPD:
13463       target = ix86_expand_unop_builtin (CODE_FOR_sse2_movapd, arglist,
13464                                          gen_reg_rtx (V2DFmode), 1);
13465       emit_insn (gen_sse2_shufpd (target, target, target, const1_rtx));
13466       return target;
13467
13468     case IX86_BUILTIN_LOADPD1:
13469       target = ix86_expand_unop_builtin (CODE_FOR_sse2_loadsd, arglist,
13470                                          gen_reg_rtx (V2DFmode), 1);
13471       emit_insn (gen_sse2_shufpd (target, target, target, const0_rtx));
13472       return target;
13473
13474     case IX86_BUILTIN_STOREPD1:
13475       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
13476     case IX86_BUILTIN_STORERPD:
13477       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
13478
13479     case IX86_BUILTIN_CLRPD:
13480       target = gen_reg_rtx (V2DFmode);
13481       emit_insn (gen_sse_clrv2df (target));
13482       return target;
13483
13484     case IX86_BUILTIN_MFENCE:
13485         emit_insn (gen_sse2_mfence ());
13486         return 0;
13487     case IX86_BUILTIN_LFENCE:
13488         emit_insn (gen_sse2_lfence ());
13489         return 0;
13490
13491     case IX86_BUILTIN_CLFLUSH:
13492         arg0 = TREE_VALUE (arglist);
13493         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13494         icode = CODE_FOR_sse2_clflush;
13495         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
13496             op0 = copy_to_mode_reg (Pmode, op0);
13497
13498         emit_insn (gen_sse2_clflush (op0));
13499         return 0;
13500
13501     case IX86_BUILTIN_MOVNTPD:
13502       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
13503     case IX86_BUILTIN_MOVNTDQ:
13504       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
13505     case IX86_BUILTIN_MOVNTI:
13506       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
13507
13508     case IX86_BUILTIN_LOADDQA:
13509       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqa, arglist, target, 1);
13510     case IX86_BUILTIN_LOADDQU:
13511       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
13512     case IX86_BUILTIN_LOADD:
13513       return ix86_expand_unop_builtin (CODE_FOR_sse2_loadd, arglist, target, 1);
13514
13515     case IX86_BUILTIN_STOREDQA:
13516       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqa, arglist);
13517     case IX86_BUILTIN_STOREDQU:
13518       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
13519     case IX86_BUILTIN_STORED:
13520       return ix86_expand_store_builtin (CODE_FOR_sse2_stored, arglist);
13521
13522     case IX86_BUILTIN_MONITOR:
13523       arg0 = TREE_VALUE (arglist);
13524       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13525       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
13526       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13527       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13528       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
13529       if (!REG_P (op0))
13530         op0 = copy_to_mode_reg (SImode, op0);
13531       if (!REG_P (op1))
13532         op1 = copy_to_mode_reg (SImode, op1);
13533       if (!REG_P (op2))
13534         op2 = copy_to_mode_reg (SImode, op2);
13535       emit_insn (gen_monitor (op0, op1, op2));
13536       return 0;
13537
13538     case IX86_BUILTIN_MWAIT:
13539       arg0 = TREE_VALUE (arglist);
13540       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13541       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13542       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13543       if (!REG_P (op0))
13544         op0 = copy_to_mode_reg (SImode, op0);
13545       if (!REG_P (op1))
13546         op1 = copy_to_mode_reg (SImode, op1);
13547       emit_insn (gen_mwait (op0, op1));
13548       return 0;
13549
13550     case IX86_BUILTIN_LOADDDUP:
13551       return ix86_expand_unop_builtin (CODE_FOR_loadddup, arglist, target, 1);
13552
13553     case IX86_BUILTIN_LDDQU:
13554       return ix86_expand_unop_builtin (CODE_FOR_lddqu, arglist, target,
13555                                        1);
13556
13557     default:
13558       break;
13559     }
13560
13561   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13562     if (d->code == fcode)
13563       {
13564         /* Compares are treated specially.  */
13565         if (d->icode == CODE_FOR_maskcmpv4sf3
13566             || d->icode == CODE_FOR_vmmaskcmpv4sf3
13567             || d->icode == CODE_FOR_maskncmpv4sf3
13568             || d->icode == CODE_FOR_vmmaskncmpv4sf3
13569             || d->icode == CODE_FOR_maskcmpv2df3
13570             || d->icode == CODE_FOR_vmmaskcmpv2df3
13571             || d->icode == CODE_FOR_maskncmpv2df3
13572             || d->icode == CODE_FOR_vmmaskncmpv2df3)
13573           return ix86_expand_sse_compare (d, arglist, target);
13574
13575         return ix86_expand_binop_builtin (d->icode, arglist, target);
13576       }
13577
13578   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13579     if (d->code == fcode)
13580       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
13581
13582   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
13583     if (d->code == fcode)
13584       return ix86_expand_sse_comi (d, arglist, target);
13585
13586   /* @@@ Should really do something sensible here.  */
13587   return 0;
13588 }
13589
13590 /* Store OPERAND to the memory after reload is completed.  This means
13591    that we can't easily use assign_stack_local.  */
13592 rtx
13593 ix86_force_to_memory (enum machine_mode mode, rtx operand)
13594 {
13595   rtx result;
13596   if (!reload_completed)
13597     abort ();
13598   if (TARGET_RED_ZONE)
13599     {
13600       result = gen_rtx_MEM (mode,
13601                             gen_rtx_PLUS (Pmode,
13602                                           stack_pointer_rtx,
13603                                           GEN_INT (-RED_ZONE_SIZE)));
13604       emit_move_insn (result, operand);
13605     }
13606   else if (!TARGET_RED_ZONE && TARGET_64BIT)
13607     {
13608       switch (mode)
13609         {
13610         case HImode:
13611         case SImode:
13612           operand = gen_lowpart (DImode, operand);
13613           /* FALLTHRU */
13614         case DImode:
13615           emit_insn (
13616                       gen_rtx_SET (VOIDmode,
13617                                    gen_rtx_MEM (DImode,
13618                                                 gen_rtx_PRE_DEC (DImode,
13619                                                         stack_pointer_rtx)),
13620                                    operand));
13621           break;
13622         default:
13623           abort ();
13624         }
13625       result = gen_rtx_MEM (mode, stack_pointer_rtx);
13626     }
13627   else
13628     {
13629       switch (mode)
13630         {
13631         case DImode:
13632           {
13633             rtx operands[2];
13634             split_di (&operand, 1, operands, operands + 1);
13635             emit_insn (
13636                         gen_rtx_SET (VOIDmode,
13637                                      gen_rtx_MEM (SImode,
13638                                                   gen_rtx_PRE_DEC (Pmode,
13639                                                         stack_pointer_rtx)),
13640                                      operands[1]));
13641             emit_insn (
13642                         gen_rtx_SET (VOIDmode,
13643                                      gen_rtx_MEM (SImode,
13644                                                   gen_rtx_PRE_DEC (Pmode,
13645                                                         stack_pointer_rtx)),
13646                                      operands[0]));
13647           }
13648           break;
13649         case HImode:
13650           /* It is better to store HImodes as SImodes.  */
13651           if (!TARGET_PARTIAL_REG_STALL)
13652             operand = gen_lowpart (SImode, operand);
13653           /* FALLTHRU */
13654         case SImode:
13655           emit_insn (
13656                       gen_rtx_SET (VOIDmode,
13657                                    gen_rtx_MEM (GET_MODE (operand),
13658                                                 gen_rtx_PRE_DEC (SImode,
13659                                                         stack_pointer_rtx)),
13660                                    operand));
13661           break;
13662         default:
13663           abort ();
13664         }
13665       result = gen_rtx_MEM (mode, stack_pointer_rtx);
13666     }
13667   return result;
13668 }
13669
13670 /* Free operand from the memory.  */
13671 void
13672 ix86_free_from_memory (enum machine_mode mode)
13673 {
13674   if (!TARGET_RED_ZONE)
13675     {
13676       int size;
13677
13678       if (mode == DImode || TARGET_64BIT)
13679         size = 8;
13680       else if (mode == HImode && TARGET_PARTIAL_REG_STALL)
13681         size = 2;
13682       else
13683         size = 4;
13684       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
13685          to pop or add instruction if registers are available.  */
13686       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13687                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
13688                                             GEN_INT (size))));
13689     }
13690 }
13691
13692 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
13693    QImode must go into class Q_REGS.
13694    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
13695    movdf to do mem-to-mem moves through integer regs.  */
13696 enum reg_class
13697 ix86_preferred_reload_class (rtx x, enum reg_class class)
13698 {
13699   if (GET_CODE (x) == CONST_VECTOR && x != CONST0_RTX (GET_MODE (x)))
13700     return NO_REGS;
13701   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
13702     {
13703       /* SSE can't load any constant directly yet.  */
13704       if (SSE_CLASS_P (class))
13705         return NO_REGS;
13706       /* Floats can load 0 and 1.  */
13707       if (MAYBE_FLOAT_CLASS_P (class) && standard_80387_constant_p (x))
13708         {
13709           /* Limit class to non-SSE.  Use GENERAL_REGS if possible.  */
13710           if (MAYBE_SSE_CLASS_P (class))
13711             return (reg_class_subset_p (class, GENERAL_REGS)
13712                     ? GENERAL_REGS : FLOAT_REGS);
13713           else
13714             return class;
13715         }
13716       /* General regs can load everything.  */
13717       if (reg_class_subset_p (class, GENERAL_REGS))
13718         return GENERAL_REGS;
13719       /* In case we haven't resolved FLOAT or SSE yet, give up.  */
13720       if (MAYBE_FLOAT_CLASS_P (class) || MAYBE_SSE_CLASS_P (class))
13721         return NO_REGS;
13722     }
13723   if (MAYBE_MMX_CLASS_P (class) && CONSTANT_P (x))
13724     return NO_REGS;
13725   if (GET_MODE (x) == QImode && ! reg_class_subset_p (class, Q_REGS))
13726     return Q_REGS;
13727   return class;
13728 }
13729
13730 /* If we are copying between general and FP registers, we need a memory
13731    location. The same is true for SSE and MMX registers.
13732
13733    The macro can't work reliably when one of the CLASSES is class containing
13734    registers from multiple units (SSE, MMX, integer).  We avoid this by never
13735    combining those units in single alternative in the machine description.
13736    Ensure that this constraint holds to avoid unexpected surprises.
13737
13738    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
13739    enforce these sanity checks.  */
13740 int
13741 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
13742                               enum machine_mode mode, int strict)
13743 {
13744   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
13745       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
13746       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
13747       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
13748       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
13749       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
13750     {
13751       if (strict)
13752         abort ();
13753       else
13754         return 1;
13755     }
13756   return (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2)
13757           || ((SSE_CLASS_P (class1) != SSE_CLASS_P (class2)
13758                || MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
13759               && ((mode != SImode && (mode != DImode || !TARGET_64BIT))
13760                   || (!TARGET_INTER_UNIT_MOVES && !optimize_size))));
13761 }
13762 /* Return the cost of moving data from a register in class CLASS1 to
13763    one in class CLASS2.
13764
13765    It is not required that the cost always equal 2 when FROM is the same as TO;
13766    on some machines it is expensive to move between registers if they are not
13767    general registers.  */
13768 int
13769 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
13770                          enum reg_class class2)
13771 {
13772   /* In case we require secondary memory, compute cost of the store followed
13773      by load.  In order to avoid bad register allocation choices, we need
13774      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
13775
13776   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
13777     {
13778       int cost = 1;
13779
13780       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
13781                    MEMORY_MOVE_COST (mode, class1, 1));
13782       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
13783                    MEMORY_MOVE_COST (mode, class2, 1));
13784
13785       /* In case of copying from general_purpose_register we may emit multiple
13786          stores followed by single load causing memory size mismatch stall.
13787          Count this as arbitrarily high cost of 20.  */
13788       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
13789         cost += 20;
13790
13791       /* In the case of FP/MMX moves, the registers actually overlap, and we
13792          have to switch modes in order to treat them differently.  */
13793       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
13794           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
13795         cost += 20;
13796
13797       return cost;
13798     }
13799
13800   /* Moves between SSE/MMX and integer unit are expensive.  */
13801   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
13802       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
13803     return ix86_cost->mmxsse_to_integer;
13804   if (MAYBE_FLOAT_CLASS_P (class1))
13805     return ix86_cost->fp_move;
13806   if (MAYBE_SSE_CLASS_P (class1))
13807     return ix86_cost->sse_move;
13808   if (MAYBE_MMX_CLASS_P (class1))
13809     return ix86_cost->mmx_move;
13810   return 2;
13811 }
13812
13813 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
13814 int
13815 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
13816 {
13817   /* Flags and only flags can only hold CCmode values.  */
13818   if (CC_REGNO_P (regno))
13819     return GET_MODE_CLASS (mode) == MODE_CC;
13820   if (GET_MODE_CLASS (mode) == MODE_CC
13821       || GET_MODE_CLASS (mode) == MODE_RANDOM
13822       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
13823     return 0;
13824   if (FP_REGNO_P (regno))
13825     return VALID_FP_MODE_P (mode);
13826   if (SSE_REGNO_P (regno))
13827     return (TARGET_SSE ? VALID_SSE_REG_MODE (mode) : 0);
13828   if (MMX_REGNO_P (regno))
13829     return (TARGET_MMX
13830             ? VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode) : 0);
13831   /* We handle both integer and floats in the general purpose registers.
13832      In future we should be able to handle vector modes as well.  */
13833   if (!VALID_INT_MODE_P (mode) && !VALID_FP_MODE_P (mode))
13834     return 0;
13835   /* Take care for QImode values - they can be in non-QI regs, but then
13836      they do cause partial register stalls.  */
13837   if (regno < 4 || mode != QImode || TARGET_64BIT)
13838     return 1;
13839   return reload_in_progress || reload_completed || !TARGET_PARTIAL_REG_STALL;
13840 }
13841
13842 /* Return the cost of moving data of mode M between a
13843    register and memory.  A value of 2 is the default; this cost is
13844    relative to those in `REGISTER_MOVE_COST'.
13845
13846    If moving between registers and memory is more expensive than
13847    between two registers, you should define this macro to express the
13848    relative cost.
13849
13850    Model also increased moving costs of QImode registers in non
13851    Q_REGS classes.
13852  */
13853 int
13854 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
13855 {
13856   if (FLOAT_CLASS_P (class))
13857     {
13858       int index;
13859       switch (mode)
13860         {
13861           case SFmode:
13862             index = 0;
13863             break;
13864           case DFmode:
13865             index = 1;
13866             break;
13867           case XFmode:
13868             index = 2;
13869             break;
13870           default:
13871             return 100;
13872         }
13873       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
13874     }
13875   if (SSE_CLASS_P (class))
13876     {
13877       int index;
13878       switch (GET_MODE_SIZE (mode))
13879         {
13880           case 4:
13881             index = 0;
13882             break;
13883           case 8:
13884             index = 1;
13885             break;
13886           case 16:
13887             index = 2;
13888             break;
13889           default:
13890             return 100;
13891         }
13892       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
13893     }
13894   if (MMX_CLASS_P (class))
13895     {
13896       int index;
13897       switch (GET_MODE_SIZE (mode))
13898         {
13899           case 4:
13900             index = 0;
13901             break;
13902           case 8:
13903             index = 1;
13904             break;
13905           default:
13906             return 100;
13907         }
13908       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
13909     }
13910   switch (GET_MODE_SIZE (mode))
13911     {
13912       case 1:
13913         if (in)
13914           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
13915                   : ix86_cost->movzbl_load);
13916         else
13917           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
13918                   : ix86_cost->int_store[0] + 4);
13919         break;
13920       case 2:
13921         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
13922       default:
13923         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
13924         if (mode == TFmode)
13925           mode = XFmode;
13926         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
13927                 * (((int) GET_MODE_SIZE (mode)
13928                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
13929     }
13930 }
13931
13932 /* Compute a (partial) cost for rtx X.  Return true if the complete
13933    cost has been computed, and false if subexpressions should be
13934    scanned.  In either case, *TOTAL contains the cost result.  */
13935
13936 static bool
13937 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
13938 {
13939   enum machine_mode mode = GET_MODE (x);
13940
13941   switch (code)
13942     {
13943     case CONST_INT:
13944     case CONST:
13945     case LABEL_REF:
13946     case SYMBOL_REF:
13947       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
13948         *total = 3;
13949       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
13950         *total = 2;
13951       else if (flag_pic && SYMBOLIC_CONST (x)
13952                && (!TARGET_64BIT
13953                    || (!GET_CODE (x) != LABEL_REF
13954                        && (GET_CODE (x) != SYMBOL_REF
13955                            || !SYMBOL_REF_LOCAL_P (x)))))
13956         *total = 1;
13957       else
13958         *total = 0;
13959       return true;
13960
13961     case CONST_DOUBLE:
13962       if (mode == VOIDmode)
13963         *total = 0;
13964       else
13965         switch (standard_80387_constant_p (x))
13966           {
13967           case 1: /* 0.0 */
13968             *total = 1;
13969             break;
13970           default: /* Other constants */
13971             *total = 2;
13972             break;
13973           case 0:
13974           case -1:
13975             /* Start with (MEM (SYMBOL_REF)), since that's where
13976                it'll probably end up.  Add a penalty for size.  */
13977             *total = (COSTS_N_INSNS (1)
13978                       + (flag_pic != 0 && !TARGET_64BIT)
13979                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
13980             break;
13981           }
13982       return true;
13983
13984     case ZERO_EXTEND:
13985       /* The zero extensions is often completely free on x86_64, so make
13986          it as cheap as possible.  */
13987       if (TARGET_64BIT && mode == DImode
13988           && GET_MODE (XEXP (x, 0)) == SImode)
13989         *total = 1;
13990       else if (TARGET_ZERO_EXTEND_WITH_AND)
13991         *total = COSTS_N_INSNS (ix86_cost->add);
13992       else
13993         *total = COSTS_N_INSNS (ix86_cost->movzx);
13994       return false;
13995
13996     case SIGN_EXTEND:
13997       *total = COSTS_N_INSNS (ix86_cost->movsx);
13998       return false;
13999
14000     case ASHIFT:
14001       if (GET_CODE (XEXP (x, 1)) == CONST_INT
14002           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
14003         {
14004           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
14005           if (value == 1)
14006             {
14007               *total = COSTS_N_INSNS (ix86_cost->add);
14008               return false;
14009             }
14010           if ((value == 2 || value == 3)
14011               && ix86_cost->lea <= ix86_cost->shift_const)
14012             {
14013               *total = COSTS_N_INSNS (ix86_cost->lea);
14014               return false;
14015             }
14016         }
14017       /* FALLTHRU */
14018
14019     case ROTATE:
14020     case ASHIFTRT:
14021     case LSHIFTRT:
14022     case ROTATERT:
14023       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
14024         {
14025           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
14026             {
14027               if (INTVAL (XEXP (x, 1)) > 32)
14028                 *total = COSTS_N_INSNS(ix86_cost->shift_const + 2);
14029               else
14030                 *total = COSTS_N_INSNS(ix86_cost->shift_const * 2);
14031             }
14032           else
14033             {
14034               if (GET_CODE (XEXP (x, 1)) == AND)
14035                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 2);
14036               else
14037                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 6 + 2);
14038             }
14039         }
14040       else
14041         {
14042           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
14043             *total = COSTS_N_INSNS (ix86_cost->shift_const);
14044           else
14045             *total = COSTS_N_INSNS (ix86_cost->shift_var);
14046         }
14047       return false;
14048
14049     case MULT:
14050       if (FLOAT_MODE_P (mode))
14051         {
14052           *total = COSTS_N_INSNS (ix86_cost->fmul);
14053           return false;
14054         }
14055       else
14056         {
14057           rtx op0 = XEXP (x, 0);
14058           rtx op1 = XEXP (x, 1);
14059           int nbits;
14060           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
14061             {
14062               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
14063               for (nbits = 0; value != 0; value &= value - 1)
14064                 nbits++;
14065             }
14066           else
14067             /* This is arbitrary.  */
14068             nbits = 7;
14069
14070           /* Compute costs correctly for widening multiplication.  */
14071           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
14072               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
14073                  == GET_MODE_SIZE (mode))
14074             {
14075               int is_mulwiden = 0;
14076               enum machine_mode inner_mode = GET_MODE (op0);
14077
14078               if (GET_CODE (op0) == GET_CODE (op1))
14079                 is_mulwiden = 1, op1 = XEXP (op1, 0);
14080               else if (GET_CODE (op1) == CONST_INT)
14081                 {
14082                   if (GET_CODE (op0) == SIGN_EXTEND)
14083                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
14084                                   == INTVAL (op1);
14085                   else
14086                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
14087                 }
14088
14089               if (is_mulwiden)
14090                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
14091             }
14092
14093           *total = COSTS_N_INSNS (ix86_cost->mult_init[MODE_INDEX (mode)]
14094                                   + nbits * ix86_cost->mult_bit)
14095                    + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code);
14096
14097           return true;
14098         }
14099
14100     case DIV:
14101     case UDIV:
14102     case MOD:
14103     case UMOD:
14104       if (FLOAT_MODE_P (mode))
14105         *total = COSTS_N_INSNS (ix86_cost->fdiv);
14106       else
14107         *total = COSTS_N_INSNS (ix86_cost->divide[MODE_INDEX (mode)]);
14108       return false;
14109
14110     case PLUS:
14111       if (FLOAT_MODE_P (mode))
14112         *total = COSTS_N_INSNS (ix86_cost->fadd);
14113       else if (GET_MODE_CLASS (mode) == MODE_INT
14114                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
14115         {
14116           if (GET_CODE (XEXP (x, 0)) == PLUS
14117               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
14118               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
14119               && CONSTANT_P (XEXP (x, 1)))
14120             {
14121               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
14122               if (val == 2 || val == 4 || val == 8)
14123                 {
14124                   *total = COSTS_N_INSNS (ix86_cost->lea);
14125                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
14126                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
14127                                       outer_code);
14128                   *total += rtx_cost (XEXP (x, 1), outer_code);
14129                   return true;
14130                 }
14131             }
14132           else if (GET_CODE (XEXP (x, 0)) == MULT
14133                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
14134             {
14135               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
14136               if (val == 2 || val == 4 || val == 8)
14137                 {
14138                   *total = COSTS_N_INSNS (ix86_cost->lea);
14139                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
14140                   *total += rtx_cost (XEXP (x, 1), outer_code);
14141                   return true;
14142                 }
14143             }
14144           else if (GET_CODE (XEXP (x, 0)) == PLUS)
14145             {
14146               *total = COSTS_N_INSNS (ix86_cost->lea);
14147               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
14148               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
14149               *total += rtx_cost (XEXP (x, 1), outer_code);
14150               return true;
14151             }
14152         }
14153       /* FALLTHRU */
14154
14155     case MINUS:
14156       if (FLOAT_MODE_P (mode))
14157         {
14158           *total = COSTS_N_INSNS (ix86_cost->fadd);
14159           return false;
14160         }
14161       /* FALLTHRU */
14162
14163     case AND:
14164     case IOR:
14165     case XOR:
14166       if (!TARGET_64BIT && mode == DImode)
14167         {
14168           *total = (COSTS_N_INSNS (ix86_cost->add) * 2
14169                     + (rtx_cost (XEXP (x, 0), outer_code)
14170                        << (GET_MODE (XEXP (x, 0)) != DImode))
14171                     + (rtx_cost (XEXP (x, 1), outer_code)
14172                        << (GET_MODE (XEXP (x, 1)) != DImode)));
14173           return true;
14174         }
14175       /* FALLTHRU */
14176
14177     case NEG:
14178       if (FLOAT_MODE_P (mode))
14179         {
14180           *total = COSTS_N_INSNS (ix86_cost->fchs);
14181           return false;
14182         }
14183       /* FALLTHRU */
14184
14185     case NOT:
14186       if (!TARGET_64BIT && mode == DImode)
14187         *total = COSTS_N_INSNS (ix86_cost->add * 2);
14188       else
14189         *total = COSTS_N_INSNS (ix86_cost->add);
14190       return false;
14191
14192     case FLOAT_EXTEND:
14193       if (!TARGET_SSE_MATH || !VALID_SSE_REG_MODE (mode))
14194         *total = 0;
14195       return false;
14196
14197     case ABS:
14198       if (FLOAT_MODE_P (mode))
14199         *total = COSTS_N_INSNS (ix86_cost->fabs);
14200       return false;
14201
14202     case SQRT:
14203       if (FLOAT_MODE_P (mode))
14204         *total = COSTS_N_INSNS (ix86_cost->fsqrt);
14205       return false;
14206
14207     case UNSPEC:
14208       if (XINT (x, 1) == UNSPEC_TP)
14209         *total = 0;
14210       return false;
14211
14212     default:
14213       return false;
14214     }
14215 }
14216
14217 #if defined (DO_GLOBAL_CTORS_BODY) && defined (HAS_INIT_SECTION)
14218 static void
14219 ix86_svr3_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
14220 {
14221   init_section ();
14222   fputs ("\tpushl $", asm_out_file);
14223   assemble_name (asm_out_file, XSTR (symbol, 0));
14224   fputc ('\n', asm_out_file);
14225 }
14226 #endif
14227
14228 #if TARGET_MACHO
14229
14230 static int current_machopic_label_num;
14231
14232 /* Given a symbol name and its associated stub, write out the
14233    definition of the stub.  */
14234
14235 void
14236 machopic_output_stub (FILE *file, const char *symb, const char *stub)
14237 {
14238   unsigned int length;
14239   char *binder_name, *symbol_name, lazy_ptr_name[32];
14240   int label = ++current_machopic_label_num;
14241
14242   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
14243   symb = (*targetm.strip_name_encoding) (symb);
14244
14245   length = strlen (stub);
14246   binder_name = alloca (length + 32);
14247   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
14248
14249   length = strlen (symb);
14250   symbol_name = alloca (length + 32);
14251   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
14252
14253   sprintf (lazy_ptr_name, "L%d$lz", label);
14254
14255   if (MACHOPIC_PURE)
14256     machopic_picsymbol_stub_section ();
14257   else
14258     machopic_symbol_stub_section ();
14259
14260   fprintf (file, "%s:\n", stub);
14261   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14262
14263   if (MACHOPIC_PURE)
14264     {
14265       fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%eax\n", label, label);
14266       fprintf (file, "\tmovl %s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
14267       fprintf (file, "\tjmp %%edx\n");
14268     }
14269   else
14270     fprintf (file, "\tjmp *%s\n", lazy_ptr_name);
14271
14272   fprintf (file, "%s:\n", binder_name);
14273
14274   if (MACHOPIC_PURE)
14275     {
14276       fprintf (file, "\tlea %s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
14277       fprintf (file, "\tpushl %%eax\n");
14278     }
14279   else
14280     fprintf (file, "\t pushl $%s\n", lazy_ptr_name);
14281
14282   fprintf (file, "\tjmp dyld_stub_binding_helper\n");
14283
14284   machopic_lazy_symbol_ptr_section ();
14285   fprintf (file, "%s:\n", lazy_ptr_name);
14286   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14287   fprintf (file, "\t.long %s\n", binder_name);
14288 }
14289 #endif /* TARGET_MACHO */
14290
14291 /* Order the registers for register allocator.  */
14292
14293 void
14294 x86_order_regs_for_local_alloc (void)
14295 {
14296    int pos = 0;
14297    int i;
14298
14299    /* First allocate the local general purpose registers.  */
14300    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
14301      if (GENERAL_REGNO_P (i) && call_used_regs[i])
14302         reg_alloc_order [pos++] = i;
14303
14304    /* Global general purpose registers.  */
14305    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
14306      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
14307         reg_alloc_order [pos++] = i;
14308
14309    /* x87 registers come first in case we are doing FP math
14310       using them.  */
14311    if (!TARGET_SSE_MATH)
14312      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
14313        reg_alloc_order [pos++] = i;
14314
14315    /* SSE registers.  */
14316    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
14317      reg_alloc_order [pos++] = i;
14318    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
14319      reg_alloc_order [pos++] = i;
14320
14321    /* x87 registers.  */
14322    if (TARGET_SSE_MATH)
14323      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
14324        reg_alloc_order [pos++] = i;
14325
14326    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
14327      reg_alloc_order [pos++] = i;
14328
14329    /* Initialize the rest of array as we do not allocate some registers
14330       at all.  */
14331    while (pos < FIRST_PSEUDO_REGISTER)
14332      reg_alloc_order [pos++] = 0;
14333 }
14334
14335 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
14336 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
14337 #endif
14338
14339 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
14340    struct attribute_spec.handler.  */
14341 static tree
14342 ix86_handle_struct_attribute (tree *node, tree name,
14343                               tree args ATTRIBUTE_UNUSED,
14344                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
14345 {
14346   tree *type = NULL;
14347   if (DECL_P (*node))
14348     {
14349       if (TREE_CODE (*node) == TYPE_DECL)
14350         type = &TREE_TYPE (*node);
14351     }
14352   else
14353     type = node;
14354
14355   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
14356                  || TREE_CODE (*type) == UNION_TYPE)))
14357     {
14358       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
14359       *no_add_attrs = true;
14360     }
14361
14362   else if ((is_attribute_p ("ms_struct", name)
14363             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
14364            || ((is_attribute_p ("gcc_struct", name)
14365                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
14366     {
14367       warning ("`%s' incompatible attribute ignored",
14368                IDENTIFIER_POINTER (name));
14369       *no_add_attrs = true;
14370     }
14371
14372   return NULL_TREE;
14373 }
14374
14375 static bool
14376 ix86_ms_bitfield_layout_p (tree record_type)
14377 {
14378   return (TARGET_USE_MS_BITFIELD_LAYOUT &&
14379           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
14380     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
14381 }
14382
14383 /* Returns an expression indicating where the this parameter is
14384    located on entry to the FUNCTION.  */
14385
14386 static rtx
14387 x86_this_parameter (tree function)
14388 {
14389   tree type = TREE_TYPE (function);
14390
14391   if (TARGET_64BIT)
14392     {
14393       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
14394       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
14395     }
14396
14397   if (ix86_function_regparm (type, function) > 0)
14398     {
14399       tree parm;
14400
14401       parm = TYPE_ARG_TYPES (type);
14402       /* Figure out whether or not the function has a variable number of
14403          arguments.  */
14404       for (; parm; parm = TREE_CHAIN (parm))
14405         if (TREE_VALUE (parm) == void_type_node)
14406           break;
14407       /* If not, the this parameter is in the first argument.  */
14408       if (parm)
14409         {
14410           int regno = 0;
14411           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
14412             regno = 2;
14413           return gen_rtx_REG (SImode, regno);
14414         }
14415     }
14416
14417   if (aggregate_value_p (TREE_TYPE (type), type))
14418     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
14419   else
14420     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
14421 }
14422
14423 /* Determine whether x86_output_mi_thunk can succeed.  */
14424
14425 static bool
14426 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
14427                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
14428                          HOST_WIDE_INT vcall_offset, tree function)
14429 {
14430   /* 64-bit can handle anything.  */
14431   if (TARGET_64BIT)
14432     return true;
14433
14434   /* For 32-bit, everything's fine if we have one free register.  */
14435   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
14436     return true;
14437
14438   /* Need a free register for vcall_offset.  */
14439   if (vcall_offset)
14440     return false;
14441
14442   /* Need a free register for GOT references.  */
14443   if (flag_pic && !(*targetm.binds_local_p) (function))
14444     return false;
14445
14446   /* Otherwise ok.  */
14447   return true;
14448 }
14449
14450 /* Output the assembler code for a thunk function.  THUNK_DECL is the
14451    declaration for the thunk function itself, FUNCTION is the decl for
14452    the target function.  DELTA is an immediate constant offset to be
14453    added to THIS.  If VCALL_OFFSET is nonzero, the word at
14454    *(*this + vcall_offset) should be added to THIS.  */
14455
14456 static void
14457 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
14458                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
14459                      HOST_WIDE_INT vcall_offset, tree function)
14460 {
14461   rtx xops[3];
14462   rtx this = x86_this_parameter (function);
14463   rtx this_reg, tmp;
14464
14465   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
14466      pull it in now and let DELTA benefit.  */
14467   if (REG_P (this))
14468     this_reg = this;
14469   else if (vcall_offset)
14470     {
14471       /* Put the this parameter into %eax.  */
14472       xops[0] = this;
14473       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
14474       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
14475     }
14476   else
14477     this_reg = NULL_RTX;
14478
14479   /* Adjust the this parameter by a fixed constant.  */
14480   if (delta)
14481     {
14482       xops[0] = GEN_INT (delta);
14483       xops[1] = this_reg ? this_reg : this;
14484       if (TARGET_64BIT)
14485         {
14486           if (!x86_64_general_operand (xops[0], DImode))
14487             {
14488               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
14489               xops[1] = tmp;
14490               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
14491               xops[0] = tmp;
14492               xops[1] = this;
14493             }
14494           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
14495         }
14496       else
14497         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
14498     }
14499
14500   /* Adjust the this parameter by a value stored in the vtable.  */
14501   if (vcall_offset)
14502     {
14503       if (TARGET_64BIT)
14504         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
14505       else
14506         {
14507           int tmp_regno = 2 /* ECX */;
14508           if (lookup_attribute ("fastcall",
14509               TYPE_ATTRIBUTES (TREE_TYPE (function))))
14510             tmp_regno = 0 /* EAX */;
14511           tmp = gen_rtx_REG (SImode, tmp_regno);
14512         }
14513
14514       xops[0] = gen_rtx_MEM (Pmode, this_reg);
14515       xops[1] = tmp;
14516       if (TARGET_64BIT)
14517         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
14518       else
14519         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
14520
14521       /* Adjust the this parameter.  */
14522       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
14523       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
14524         {
14525           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
14526           xops[0] = GEN_INT (vcall_offset);
14527           xops[1] = tmp2;
14528           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
14529           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
14530         }
14531       xops[1] = this_reg;
14532       if (TARGET_64BIT)
14533         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
14534       else
14535         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
14536     }
14537
14538   /* If necessary, drop THIS back to its stack slot.  */
14539   if (this_reg && this_reg != this)
14540     {
14541       xops[0] = this_reg;
14542       xops[1] = this;
14543       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
14544     }
14545
14546   xops[0] = XEXP (DECL_RTL (function), 0);
14547   if (TARGET_64BIT)
14548     {
14549       if (!flag_pic || (*targetm.binds_local_p) (function))
14550         output_asm_insn ("jmp\t%P0", xops);
14551       else
14552         {
14553           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
14554           tmp = gen_rtx_CONST (Pmode, tmp);
14555           tmp = gen_rtx_MEM (QImode, tmp);
14556           xops[0] = tmp;
14557           output_asm_insn ("jmp\t%A0", xops);
14558         }
14559     }
14560   else
14561     {
14562       if (!flag_pic || (*targetm.binds_local_p) (function))
14563         output_asm_insn ("jmp\t%P0", xops);
14564       else
14565 #if TARGET_MACHO
14566         if (TARGET_MACHO)
14567           {
14568             rtx sym_ref = XEXP (DECL_RTL (function), 0);
14569             tmp = (gen_rtx_SYMBOL_REF
14570                    (Pmode,
14571                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
14572             tmp = gen_rtx_MEM (QImode, tmp);
14573             xops[0] = tmp;
14574             output_asm_insn ("jmp\t%0", xops);
14575           }
14576         else
14577 #endif /* TARGET_MACHO */
14578         {
14579           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
14580           output_set_got (tmp);
14581
14582           xops[1] = tmp;
14583           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
14584           output_asm_insn ("jmp\t{*}%1", xops);
14585         }
14586     }
14587 }
14588
14589 static void
14590 x86_file_start (void)
14591 {
14592   default_file_start ();
14593   if (X86_FILE_START_VERSION_DIRECTIVE)
14594     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
14595   if (X86_FILE_START_FLTUSED)
14596     fputs ("\t.global\t__fltused\n", asm_out_file);
14597   if (ix86_asm_dialect == ASM_INTEL)
14598     fputs ("\t.intel_syntax\n", asm_out_file);
14599 }
14600
14601 int
14602 x86_field_alignment (tree field, int computed)
14603 {
14604   enum machine_mode mode;
14605   tree type = TREE_TYPE (field);
14606
14607   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
14608     return computed;
14609   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
14610                     ? get_inner_array_type (type) : type);
14611   if (mode == DFmode || mode == DCmode
14612       || GET_MODE_CLASS (mode) == MODE_INT
14613       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
14614     return MIN (32, computed);
14615   return computed;
14616 }
14617
14618 /* Output assembler code to FILE to increment profiler label # LABELNO
14619    for profiling a function entry.  */
14620 void
14621 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
14622 {
14623   if (TARGET_64BIT)
14624     if (flag_pic)
14625       {
14626 #ifndef NO_PROFILE_COUNTERS
14627         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
14628 #endif
14629         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
14630       }
14631     else
14632       {
14633 #ifndef NO_PROFILE_COUNTERS
14634         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
14635 #endif
14636         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
14637       }
14638   else if (flag_pic)
14639     {
14640 #ifndef NO_PROFILE_COUNTERS
14641       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
14642                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
14643 #endif
14644       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
14645     }
14646   else
14647     {
14648 #ifndef NO_PROFILE_COUNTERS
14649       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
14650                PROFILE_COUNT_REGISTER);
14651 #endif
14652       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
14653     }
14654 }
14655
14656 /* We don't have exact information about the insn sizes, but we may assume
14657    quite safely that we are informed about all 1 byte insns and memory
14658    address sizes.  This is enough to eliminate unnecessary padding in
14659    99% of cases.  */
14660
14661 static int
14662 min_insn_size (rtx insn)
14663 {
14664   int l = 0;
14665
14666   if (!INSN_P (insn) || !active_insn_p (insn))
14667     return 0;
14668
14669   /* Discard alignments we've emit and jump instructions.  */
14670   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
14671       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
14672     return 0;
14673   if (GET_CODE (insn) == JUMP_INSN
14674       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
14675           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
14676     return 0;
14677
14678   /* Important case - calls are always 5 bytes.
14679      It is common to have many calls in the row.  */
14680   if (GET_CODE (insn) == CALL_INSN
14681       && symbolic_reference_mentioned_p (PATTERN (insn))
14682       && !SIBLING_CALL_P (insn))
14683     return 5;
14684   if (get_attr_length (insn) <= 1)
14685     return 1;
14686
14687   /* For normal instructions we may rely on the sizes of addresses
14688      and the presence of symbol to require 4 bytes of encoding.
14689      This is not the case for jumps where references are PC relative.  */
14690   if (GET_CODE (insn) != JUMP_INSN)
14691     {
14692       l = get_attr_length_address (insn);
14693       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
14694         l = 4;
14695     }
14696   if (l)
14697     return 1+l;
14698   else
14699     return 2;
14700 }
14701
14702 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
14703    window.  */
14704
14705 static void
14706 ix86_avoid_jump_misspredicts (void)
14707 {
14708   rtx insn, start = get_insns ();
14709   int nbytes = 0, njumps = 0;
14710   int isjump = 0;
14711
14712   /* Look for all minimal intervals of instructions containing 4 jumps.
14713      The intervals are bounded by START and INSN.  NBYTES is the total
14714      size of instructions in the interval including INSN and not including
14715      START.  When the NBYTES is smaller than 16 bytes, it is possible
14716      that the end of START and INSN ends up in the same 16byte page.
14717
14718      The smallest offset in the page INSN can start is the case where START
14719      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
14720      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
14721      */
14722   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14723     {
14724
14725       nbytes += min_insn_size (insn);
14726       if (dump_file)
14727         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
14728                 INSN_UID (insn), min_insn_size (insn));
14729       if ((GET_CODE (insn) == JUMP_INSN
14730            && GET_CODE (PATTERN (insn)) != ADDR_VEC
14731            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
14732           || GET_CODE (insn) == CALL_INSN)
14733         njumps++;
14734       else
14735         continue;
14736
14737       while (njumps > 3)
14738         {
14739           start = NEXT_INSN (start);
14740           if ((GET_CODE (start) == JUMP_INSN
14741                && GET_CODE (PATTERN (start)) != ADDR_VEC
14742                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
14743               || GET_CODE (start) == CALL_INSN)
14744             njumps--, isjump = 1;
14745           else
14746             isjump = 0;
14747           nbytes -= min_insn_size (start);
14748         }
14749       if (njumps < 0)
14750         abort ();
14751       if (dump_file)
14752         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
14753                 INSN_UID (start), INSN_UID (insn), nbytes);
14754
14755       if (njumps == 3 && isjump && nbytes < 16)
14756         {
14757           int padsize = 15 - nbytes + min_insn_size (insn);
14758
14759           if (dump_file)
14760             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
14761                      INSN_UID (insn), padsize);
14762           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
14763         }
14764     }
14765 }
14766
14767 /* AMD Athlon works faster
14768    when RET is not destination of conditional jump or directly preceded
14769    by other jump instruction.  We avoid the penalty by inserting NOP just
14770    before the RET instructions in such cases.  */
14771 static void
14772 ix86_pad_returns (void)
14773 {
14774   edge e;
14775
14776   for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
14777   {
14778     basic_block bb = e->src;
14779     rtx ret = BB_END (bb);
14780     rtx prev;
14781     bool replace = false;
14782
14783     if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
14784         || !maybe_hot_bb_p (bb))
14785       continue;
14786     for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
14787       if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
14788         break;
14789     if (prev && GET_CODE (prev) == CODE_LABEL)
14790       {
14791         edge e;
14792         for (e = bb->pred; e; e = e->pred_next)
14793           if (EDGE_FREQUENCY (e) && e->src->index >= 0
14794               && !(e->flags & EDGE_FALLTHRU))
14795             replace = true;
14796       }
14797     if (!replace)
14798       {
14799         prev = prev_active_insn (ret);
14800         if (prev
14801             && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
14802                 || GET_CODE (prev) == CALL_INSN))
14803           replace = true;
14804         /* Empty functions get branch mispredict even when the jump destination
14805            is not visible to us.  */
14806         if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
14807           replace = true;
14808       }
14809     if (replace)
14810       {
14811         emit_insn_before (gen_return_internal_long (), ret);
14812         delete_insn (ret);
14813       }
14814   }
14815 }
14816
14817 /* Implement machine specific optimizations.  We implement padding of returns
14818    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
14819 static void
14820 ix86_reorg (void)
14821 {
14822   if (TARGET_ATHLON_K8 && optimize && !optimize_size)
14823     ix86_pad_returns ();
14824   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
14825     ix86_avoid_jump_misspredicts ();
14826 }
14827
14828 /* Return nonzero when QImode register that must be represented via REX prefix
14829    is used.  */
14830 bool
14831 x86_extended_QIreg_mentioned_p (rtx insn)
14832 {
14833   int i;
14834   extract_insn_cached (insn);
14835   for (i = 0; i < recog_data.n_operands; i++)
14836     if (REG_P (recog_data.operand[i])
14837         && REGNO (recog_data.operand[i]) >= 4)
14838        return true;
14839   return false;
14840 }
14841
14842 /* Return nonzero when P points to register encoded via REX prefix.
14843    Called via for_each_rtx.  */
14844 static int
14845 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
14846 {
14847    unsigned int regno;
14848    if (!REG_P (*p))
14849      return 0;
14850    regno = REGNO (*p);
14851    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
14852 }
14853
14854 /* Return true when INSN mentions register that must be encoded using REX
14855    prefix.  */
14856 bool
14857 x86_extended_reg_mentioned_p (rtx insn)
14858 {
14859   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
14860 }
14861
14862 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
14863    optabs would emit if we didn't have TFmode patterns.  */
14864
14865 void
14866 x86_emit_floatuns (rtx operands[2])
14867 {
14868   rtx neglab, donelab, i0, i1, f0, in, out;
14869   enum machine_mode mode, inmode;
14870
14871   inmode = GET_MODE (operands[1]);
14872   if (inmode != SImode
14873       && inmode != DImode)
14874     abort ();
14875
14876   out = operands[0];
14877   in = force_reg (inmode, operands[1]);
14878   mode = GET_MODE (out);
14879   neglab = gen_label_rtx ();
14880   donelab = gen_label_rtx ();
14881   i1 = gen_reg_rtx (Pmode);
14882   f0 = gen_reg_rtx (mode);
14883
14884   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
14885
14886   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
14887   emit_jump_insn (gen_jump (donelab));
14888   emit_barrier ();
14889
14890   emit_label (neglab);
14891
14892   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
14893   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
14894   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
14895   expand_float (f0, i0, 0);
14896   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
14897
14898   emit_label (donelab);
14899 }
14900
14901 /* Initialize vector TARGET via VALS.  */
14902 void
14903 ix86_expand_vector_init (rtx target, rtx vals)
14904 {
14905   enum machine_mode mode = GET_MODE (target);
14906   int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
14907   int n_elts = (GET_MODE_SIZE (mode) / elt_size);
14908   int i;
14909
14910   for (i = n_elts - 1; i >= 0; i--)
14911     if (GET_CODE (XVECEXP (vals, 0, i)) != CONST_INT
14912         && GET_CODE (XVECEXP (vals, 0, i)) != CONST_DOUBLE)
14913       break;
14914
14915   /* Few special cases first...
14916      ... constants are best loaded from constant pool.  */
14917   if (i < 0)
14918     {
14919       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
14920       return;
14921     }
14922
14923   /* ... values where only first field is non-constant are best loaded
14924      from the pool and overwritten via move later.  */
14925   if (!i)
14926     {
14927       rtx op = simplify_gen_subreg (mode, XVECEXP (vals, 0, 0),
14928                                     GET_MODE_INNER (mode), 0);
14929
14930       op = force_reg (mode, op);
14931       XVECEXP (vals, 0, 0) = CONST0_RTX (GET_MODE_INNER (mode));
14932       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
14933       switch (GET_MODE (target))
14934         {
14935           case V2DFmode:
14936             emit_insn (gen_sse2_movsd (target, target, op));
14937             break;
14938           case V4SFmode:
14939             emit_insn (gen_sse_movss (target, target, op));
14940             break;
14941           default:
14942             break;
14943         }
14944       return;
14945     }
14946
14947   /* And the busy sequence doing rotations.  */
14948   switch (GET_MODE (target))
14949     {
14950       case V2DFmode:
14951         {
14952           rtx vecop0 =
14953             simplify_gen_subreg (V2DFmode, XVECEXP (vals, 0, 0), DFmode, 0);
14954           rtx vecop1 =
14955             simplify_gen_subreg (V2DFmode, XVECEXP (vals, 0, 1), DFmode, 0);
14956
14957           vecop0 = force_reg (V2DFmode, vecop0);
14958           vecop1 = force_reg (V2DFmode, vecop1);
14959           emit_insn (gen_sse2_unpcklpd (target, vecop0, vecop1));
14960         }
14961         break;
14962       case V4SFmode:
14963         {
14964           rtx vecop0 =
14965             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 0), SFmode, 0);
14966           rtx vecop1 =
14967             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 1), SFmode, 0);
14968           rtx vecop2 =
14969             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 2), SFmode, 0);
14970           rtx vecop3 =
14971             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 3), SFmode, 0);
14972           rtx tmp1 = gen_reg_rtx (V4SFmode);
14973           rtx tmp2 = gen_reg_rtx (V4SFmode);
14974
14975           vecop0 = force_reg (V4SFmode, vecop0);
14976           vecop1 = force_reg (V4SFmode, vecop1);
14977           vecop2 = force_reg (V4SFmode, vecop2);
14978           vecop3 = force_reg (V4SFmode, vecop3);
14979           emit_insn (gen_sse_unpcklps (tmp1, vecop1, vecop3));
14980           emit_insn (gen_sse_unpcklps (tmp2, vecop0, vecop2));
14981           emit_insn (gen_sse_unpcklps (target, tmp2, tmp1));
14982         }
14983         break;
14984       default:
14985         abort ();
14986     }
14987 }
14988
14989 /* Implements target hook vector_mode_supported_p.  */
14990 static bool
14991 ix86_vector_mode_supported_p (enum machine_mode mode)
14992 {
14993   if (TARGET_SSE
14994       && VALID_SSE_REG_MODE (mode))
14995     return true;
14996
14997   else if (TARGET_MMX
14998            && VALID_MMX_REG_MODE (mode))
14999     return true;
15000
15001   else if (TARGET_3DNOW
15002            && VALID_MMX_REG_MODE_3DNOW (mode))
15003     return true;
15004
15005   else
15006     return false;
15007 }
15008
15009 /* Worker function for TARGET_MD_ASM_CLOBBERS.
15010
15011    We do this in the new i386 backend to maintain source compatibility
15012    with the old cc0-based compiler.  */
15013
15014 static tree
15015 ix86_md_asm_clobbers (tree clobbers)
15016 {
15017   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
15018                         clobbers);
15019   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
15020                         clobbers);
15021   clobbers = tree_cons (NULL_TREE, build_string (7, "dirflag"),
15022                         clobbers);
15023   return clobbers;
15024 }
15025
15026 /* Worker function for REVERSE_CONDITION.  */
15027
15028 enum rtx_code
15029 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
15030 {
15031   return (mode != CCFPmode && mode != CCFPUmode
15032           ? reverse_condition (code)
15033           : reverse_condition_maybe_unordered (code));
15034 }
15035
15036 /* Output code to perform an x87 FP register move, from OPERANDS[1]
15037    to OPERANDS[0].  */
15038
15039 const char *
15040 output_387_reg_move (rtx insn, rtx *operands)
15041 {
15042   if (REG_P (operands[1])
15043       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
15044     {
15045       if (REGNO (operands[0]) == FIRST_STACK_REG
15046           && TARGET_USE_FFREEP)
15047         return "ffreep\t%y0";
15048       return "fstp\t%y0";
15049     }
15050   if (STACK_TOP_P (operands[0]))
15051     return "fld%z1\t%y1";
15052   return "fst\t%y0";
15053 }
15054
15055 /* Output code to perform a conditional jump to LABEL, if C2 flag in
15056    FP status register is set.  */
15057
15058 void
15059 ix86_emit_fp_unordered_jump (rtx label)
15060 {
15061   rtx reg = gen_reg_rtx (HImode);
15062   rtx temp;
15063
15064   emit_insn (gen_x86_fnstsw_1 (reg));
15065
15066   if (TARGET_USE_SAHF)
15067     {
15068       emit_insn (gen_x86_sahf_1 (reg));
15069
15070       temp = gen_rtx_REG (CCmode, FLAGS_REG);
15071       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
15072     }
15073   else
15074     {
15075       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
15076
15077       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
15078       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
15079     }
15080
15081   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
15082                               gen_rtx_LABEL_REF (VOIDmode, label),
15083                               pc_rtx);
15084   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
15085   emit_jump_insn (temp);
15086 }
15087
15088 /* Output code to perform a log1p XFmode calculation.  */
15089
15090 void ix86_emit_i387_log1p (rtx op0, rtx op1)
15091 {
15092   rtx label1 = gen_label_rtx ();
15093   rtx label2 = gen_label_rtx ();
15094
15095   rtx tmp = gen_reg_rtx (XFmode);
15096   rtx tmp2 = gen_reg_rtx (XFmode);
15097
15098   emit_insn (gen_absxf2 (tmp, op1));
15099   emit_insn (gen_cmpxf (tmp,
15100     CONST_DOUBLE_FROM_REAL_VALUE (
15101        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
15102        XFmode)));
15103   emit_jump_insn (gen_bge (label1));
15104
15105   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
15106   emit_insn (gen_fyl2xp1_xf3 (op0, tmp2, op1));
15107   emit_jump (label2);
15108
15109   emit_label (label1);
15110   emit_move_insn (tmp, CONST1_RTX (XFmode));
15111   emit_insn (gen_addxf3 (tmp, op1, tmp));
15112   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
15113   emit_insn (gen_fyl2x_xf3 (op0, tmp2, tmp));
15114
15115   emit_label (label2);
15116 }
15117
15118 #include "gt-i386.h"